我的作业要求我找到 4 个输入数字中最大的 2 个。
我尝试了 if 语句,但我认为我的处理方式是错误的。我必须使用 if 语句或 while 循环。我该如何继续?
if ((a>b && b>c && c>d)||(a>b && b>d && d>c)){
System.out.println("max1 is : " + a);
System.out.println("max2 is : " + b);}
else if ((b>a && a>c && c>d)||(b>a && a>c && d>c) ){
System.out.println("max1 is : " + b);
System.out.println("max2 is : " + a);}
if ((c>a && a>b && b>d)||(c>a && a>b && d>b)){
System.out.println("max1 is : " + c);
System.out.println("max2 is : " + a);}
最佳答案
只需将参数放入数组中,对该数组进行排序并显示最后一个元素
public static void main(String[] args) {
dispayMax(5, -13, 7, 88);
}
public static void dispayMax(int a, int b, int c, int d){
int[] array = {a, b, c, d};
Arrays.sort(array);
int length = array.length;
System.out.println("max1 is : " + array[length -2]);
System.out.println("max2 is : " + array[length -1]);
}
关于Java if 语句作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58594208/