![](/img/trans.png)
[英]How to fix runtime error - Exception in thread “main” java.util.NoSuchElementException
[英]Exception in thread "main" java.util.NoSuchElementException (how to fix error)
這個錯誤讓我很緊張。
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!");}
}
}
如果輸入不正確,想結束整個程序。 不確定到底出了什么問題。 現在被困在這個問題上一段時間了。 您如何找出掃描儀和輸入的問題?
您的錯誤純粹是邏輯錯誤。 你在不應該出現的地方都有}。 您也沒有消耗按 Enter 鍵在 nextInt() 上生成的 /n“新行”。
為避免混淆您對嵌套 if 的自用開頭和結尾注釋 在創建 if 時創建框架工作並使用空格將所有內容隔開是一個很好的做法。
if( conditon)
{
if_statement(s);
}
else
{
else_statement(s);
}
這是您更正的代碼
import java.util.Scanner;
import java.lang.Math;
class BrockTaylor
{
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(" ");
System.out.println("PHASE 1");
System.out.println("Enter a number:");
int y = scan.nextInt();
// consume the /n
scan.nextLine();
//Phase 1
if (y == 3)
{
System.out.println("Correct!");
System.out.println(" ");
System.out.println("PHASE 2");
System.out.println("Enter a number:");
int z = scan.nextInt();
// consume the /n
scan.nextLine();
//Phase 2
if (z == 1 || z >= 33 && z <= 100)
{
System.out.println("Correct!");
System.out.println(" ");
System.out.println("PHASE 3");
System.out.println("Enter a number:");
int c = scan.nextInt();
// consume the /n
scan.nextLine();
//Phase 3
if (c % 3 == 0 || c % 7 == 0)
{
System.out.println("Correct!");
System.out.println("You have cracked the code!");
}
else // phase 3
{
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
}
else // phase 2
{
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
}
else// phase 1
{
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.