我是 Java 初学者,对我正在做的作业有疑问。
我正在尝试读取整数输入序列并打印出最大和最小的数字。虽然我已经写了代码,但问题是当我运行它时,它不会打印最大或最小的数字。该代码看起来是正确的,但事实并非如此!任何帮助将不胜感激。
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter integers: ");
int largest = in.nextInt();
int smallest = largest;
while (in.hasNextInt()) {
int input = in.nextInt();
if (input > largest) {
largest = input;
} else if (input < smallest) {
smallest = input;
}
System.out.println();
}
System.out.println(largest);
System.out.println(smallest);
}
}
最佳答案
停止等待输入: 输入一个字符作为输入:in.hasNextInt() return False
关于java - While 循环和困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18687317/