-2
這個錯誤讓我的神經。異常線程「main」 java.util.NoSuchElementException(如何修復錯誤)
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 Main.main(Main.java:266)
at Ideone.test(Main.java:72)
at Ideone.test(Main.java:32)
at Ideone.main(Main.java:23)
我的代碼:
import java.util.Scanner;
import java.lang.Math;
class Main {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Welcome. What is your name?");
String x;
x = scan.nextLine();
System.out.println("Hello " + x + ". Try your best to crack the code!");
System.out.println(" ");
//Phase 1
System.out.println("PHASE 1");
System.out.println("Enter a number:");
int y = scan.nextInt();
if (y == 3){
System.out.println("Correct!");
System.out.println(" ");
//Phase 2
System.out.println("PHASE 2");
System.out.println("Enter a number:");
}
int z = scan.nextInt();
if (z == 1 || (z >= 33 && z <= 100)){
System.out.println("Correct!");
System.out.println(" ");
//Phase 3
System.out.println("PHASE 3");
System.out.println("Enter a number:");}
int c = scan.nextInt();
if (c % 3 == 0 || c % 7 == 0){
System.out.println("Correct!");
System.out.println("You have cracked the code!");}
else{
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");}
}
}
要結束的整個程序,如果輸入不正確。 不知道什麼是錯的。 現在已經停留了一段時間。 如何找出掃描儀和輸入有什麼問題?
*「你如何找出什麼是錯的掃描儀和投入?」 *調用'nextInt'之前調用'hasNextInt'。 – Andreas