有人可以解释为什么当我输入-99时,程序没有停止吗? 当我第一次输入 -99 时,程序停止,但如果我先输入 1,然后第二次输入 -99,程序不会停止
import java.util.Scanner;
public class TESTTEST {
public static void main(String [] args) {
// creates the scanner
Scanner input = new Scanner(System.in);
int number;
int max=0;
int min=0;
int counter = 0;
System.out.print("Enter an integer (-99 to stop): ");
number = input.nextInt();
{
while (number != -99) {
System.out.print("Enter an integer (-99 to stop): ");
number = input.nextInt();
if (number> max ){
number = max;
}
else if (number<max ) {
number = min;
}
}
System.out.println("max value is " +max);
System.out.println("min value is "+min);
}
}
}
最佳答案
您颠倒了数字
和max
/min
if (number> max ){
number = max; //Since max = 0, number turns into 0
}
else if (number<max ) {
number = min; //Same thing here
}
}
System.out.println("max value is " +max);
System.out.println("min value is "+min);
}
切换它们:
if (number> max ){
max = number;
}
else if (number<max ) {
min= number;
}
}
System.out.println("max value is " +max);
System.out.println("min value is "+min);
}
关于Java Codelab While 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35327809/