Ich habe Probleme mit dem Verständnis, was mit diesem Code falsch ist. Auf Dr.Java funktioniert alles gut, aber auf einer anderen Code-Plattform namens edhesive (wo ich dieses Projekt zugewiesen wurde) gibt es mir einen Fehler. Ich habe alles überprüft, was ich für falsch halte, weiß aber immer noch nicht, was falsch ist.Laufzeitfehler - Ausnahme im Thread "Haupt" java.util.InputMismatchException
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Welcome. What is your name?");
String name = scan.next();
System.out.println("Hello " + name + ". Try your best to crack the code!");
System.out.println("PHASE 1");
System.out.println("Enter a number:");
int phaseOneNum = scan.nextInt();
if (phaseOneNum == 3)
{
System.out.println("Correct!");
System.out.println("PHASE 2");
System.out.println("Enter a number:");
int phaseTwoNum = scan.nextInt();
if (phaseTwoNum == 1 || (phaseTwoNum > 33 && phaseTwoNum<= 100))
{
System.out.println("Correct!");
System.out.println("PHASE 3");
System.out.println("Enter a number:");
int phaseThreeNum = scan.nextInt();
if (phaseThreeNum > 0 && ((phaseThreeNum % 3 == 0) || (phaseThreeNum % 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!");
}
}
else
{
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
}
else
{
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
}
}
Nachdem es auf edhesive läuft, bekomme ich diesen Fehler
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
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:184)
at Ideone.test(Main.java:111)
at Ideone.test(Main.java:31)
at Ideone.main(Main.java:23)
jemand mir bitte helfen?
setzen Sie einen Haltepunkt in Zeile 184 und rufen Sie Scanner.nextInt auf, sehen Sie das Ergebnis. –
Hier einige Fragen in SO können Ihnen helfen, diese Ausnahme zu verstehen: https://stackoverflow.com/questions/13408799/what-is-a-java-util-inputmismatchexception https://stackoverflow.com/questions/19460363/scanners -exception-java-util-inputmismatchexception – esprittn