此程序仅用于if语句以三个数字作为用户输入,然后按升序将它们打印到屏幕上。但是,当我尝试运行此代码时,我收到以下输出:
Enter first number: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at OrderNumbers.main(OrderNumbers.java:10)
这是我目前正在运行的代码:
import java.util.*;
public class OrderNumbers{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print("Ordering 3 whole numbers \n");
System.out.print("Enter first number: ");
int numberOne = scan.nextInt();
System.out.print("Enter second number: ");
int numberTwo = scan.nextInt();
System.out.print("Enter third number: ");
int numberThree = scan.nextInt();
if( numberOne > numberTwo && numberTwo > numberThree){
System.out.println( "Ordered numbers are: " + numberThree + ", " + numberTwo + ", " + numberOne);
}
else if( numberOne > numberTwo && numberThree > numberTwo) {
System.out.println( "Ordered numbers are: " + numberTwo + ", " + numberThree + ", " + numberOne);
}
else if( numberTwo > numberOne && numberOne > numberThree) {
System.out.println( "Ordered numbers are: " + numberThree + ", " + numberOne + ", " + numberTwo);
}
else if( numberTwo > numberOne && numberOne < numberThree) {
System.out.println( "Ordered numbers are: " + numberOne + ", " + numberThree + ", " + numberTwo);
}
else if( numberThree > numberOne && numberOne < numberTwo) {
System.out.println( "Ordered numbers are: " + numberOne + ", " + numberTwo + ", " + numberThree);
}
else if( numberThree > numberOne && numberOne > numberTwo){
System.out.println( "Ordered numbers are: " + numberTwo + ", " + numberOne + ", " + numberThree);
}
}
}
我不知道错误在哪里,但任何帮助都会受到赞赏。
答案 0 :(得分:0)
可能会抛出异常,因为您没有输入数字。您应检查输入是否为input.hasNextInt()
的数字,如果input.next()
(返回String
)不是这种情况,则应检查打印错误,以查看您从输入中获取的内容。< / p>