mar*_*bov 0 java methods boolean
我正在尝试编写检查数组是否已排序的代码,homework说它必须在带有布尔返回的方法中,但是如果我将返回放在for循环中,它会在for循环之前停止for循环只是列表中的前两位数字.无论哪种方式,即使我把循环中的返回值eclipse告诉我方法中没有布尔返回值?我不知道如何让它工作,在网上找不到任何类似的东西请帮忙:)
public static boolean isSorted (double [] list) {
for (int i=1; i<list.length; i++) {
if (list[(int) i-1] > list[(int) i] ) {
System.out.println("Not Sorted");
return false;
}
else {
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
public static boolean isSorted(double[] list) {
for (int i = 1; i < list.length; i++) {
if (list[i - 1] > list[i]) {
System.out.println("Not Sorted");
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
试试这个代码.如果排序,您将在最后返回true