接收5,9,7作为输出..预期应该只有9..Below是代码:
public class GreatestNoInArray {
public static void main(String[] args) {
int a[]= new int[] {1,2,5,9,7};
int big=a[0];
for (int i=1; i<a.length; i++){
if (big<a[i])
big=a[i];
System.out.println(a[i]);
}
}
}
请帮忙
答案 0 :(得分:7)
对于预期答案,您需要在循环中打印big
(不是a[i]
)
答案 1 :(得分:4)
public class GreatestNoInArray {
public static void main(String[] args) {
int a[]= new int[] {1,2,5,9,7};
int big=a[0];
for (int i=1; i<a.length; i++){
if (big<a[i])
big=a[i];
}
System.out.println(big);
}
}
答案 2 :(得分:0)
代码应该是这样的:
public class GreatestNoInArray {
public static void main(String[] args) {
int a[] = new int[] {1,2,5,9,7};
int big = a[0];
for (int i=1; i<a.length; i++){
if (big < a[i])
big = a[i];
//System.out.println(a[i]);
}
System.out.println("Big: " + big);
}
}
这有效!