我得到了一个 3 位数的列表,我想看看它们是否按降序排列。列表中的元素数量尚未确定,但我已将 SENTINEL
值设置为 1000
。
尽管如此,以下错误仍然发生:
CompileRunTest: throwable = java.util.NoSuchElementException
java.util.NoSuchElementException
我的代码:
import java.util.Scanner ;
public class StrictDescending {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
final int SENTINEL = 1000 ;
int firstValue = in.nextInt();
while (firstValue < 1000)
{
int secondValue = in.nextInt() ;
while(secondValue < 1000)
{
if(firstValue > secondValue)
{
System.out.println("Yes, the list is in descending order.") ;
}
else
{
System.out.println("No, the list is not in descending order.") ;
}
secondValue = in.nextInt();
}
firstValue = in.nextInt() ;
}
}
}
最佳答案
尝试将第一个 while 切换为 if 语句,在 secondValue = in.nextInt(); 之前的行添加
并删除最后一个 firstValue = secondaryValue
firstValue = in.nextInt();
您还需要稍微修改一下 print 语句。
因为它是你的程序流程不太有意义,因为即使没有剩下的数字,你也会尝试从标准输入消费。
关于java - java 程序出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37732299/