Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the maximum and minimum value in java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report