Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to find the maximum and minimum value in java

Shulou Source: shulou.com Published: 2022-06-02 10:25:57 09月16日 Update

It is believed that many inexperienced people don't know what to do about how to find the maximum and minimum value in java. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Three methods of finding the maximum value by java

General method: public class maxtest {public static void main (String [] args) {/ / # maximum and minimum start#//int [] aa = {4, 2, 17, 5, 22, 8, 13, 6}; int max = aa [0]; int min = aa [0]; for (int I = 0; I)

< aa.length-1; i++) { if(aa[i] >

Max) {max = aa [I];}} System.out.println ("maximum" + max); for (int I = 0; I

< aa.length-1; i++) { if(aa[i] < min){ min = aa[i]; } } System.out.println("最小值"+min); //###################最大值最小值end###################// }} 结果值 最大值22最小值2三元运算符方法:public class maxtest { public static void main(String[] args) { //###################三元运算符最大值最小值start###################//int[] aa = {4,2,17,5,22,8,13,6}; int max = aa[0]; int min = aa[0]; for (int i = 0; i < aa.length; i++) { max = (aa[i] < max ? max : aa[i]); } System.out.println("最大值"+max); for (int i = 0; i < aa.length; i++) { min = (aa[i] >

Min? Min: aa [I]);} System.out.println ("minimum" + min); / / # maximum and minimum of ternary operators start#//}}

Result value

Maximum value 22 minimum value 2 boring writing method: public class maxtest {public static void main (String [] args) {/ / # maximum value (find separately) start#//int [] aa = {4, 2, 17, 14, 6, 12, 12, 15, 122, 18, 18, 16, 24, 22, 22, 22, 22, 22, 22, 22, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 14, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 18, 216, 16, 16, 22, 22, 22 Int bb = aa.length/2; int bbmax = aa [0]; for (int I = 0; I

< bb-1; i++) { if(aa[i] >

Bbmax) {bbmax = aa [I];}} int ccmax = aa [bb]; for (int I = 0; I

< aa.length-1; i++) { if(aa[i] >

Ccmax) {ccmax = aa [I];}} System.out.println ("left maximum:" + bbmax); System.out.println ("right maximum:" + ccmax) If (bbmax > ccmax) {System.out.println ("result value: + bbmax);} else {System.out.println (" result value: "+ ccmax) } / / # maximum and minimum (separate search) end#//}}

End value

Left maximum: 122Rank maximum: 222.Result value: 222.After reading the above, have you mastered the method of how to find the maximum and minimum value in java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Maximum maximum minimum method result operator ternary operation content more problem boredom helpless for this reason for this reason skills articles experience industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei Xiaomi vpn Apple