2017-10-14 4 views
-1

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?

+1

setzen Sie einen Haltepunkt in Zeile 184 und rufen Sie Scanner.nextInt auf, sehen Sie das Ergebnis. –

+0

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

Antwort

0

Laufzeitfehler - Exception in thread „main“ java.util.InputMismatchException

scan.nextInt(); integer akzeptieren nur, können Sie vorbei String. So gibt es 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 = 0; 
     do { 
      while (!scan.hasNextInt()) { 
       System.out.println("That's not a number!"); 
       scan.next(); // this is important! 
      } 
      phaseOneNum = scan.nextInt(); 
     } while (phaseOneNum <= 0); 

     if (phaseOneNum == 3) { 
      System.out.println("Correct!"); 

      System.out.println("PHASE 2"); 
      System.out.println("Enter a number:"); 
      int phaseTwoNum; 
      do { 
       while (!scan.hasNextInt()) { 
        System.out.println("That's not a number!"); 
        scan.next(); // this is important! 
       } 
       phaseTwoNum = scan.nextInt(); 
      } while (phaseTwoNum <= 0); 

      if (phaseTwoNum == 1 || (phaseTwoNum > 33 && phaseTwoNum <= 100)) { 
       System.out.println("Correct!"); 

       System.out.println("PHASE 3"); 
       System.out.println("Enter a number:"); 
       int phaseThreeNum; 
     do { 
      while (!scan.hasNextInt()) { 
       System.out.println("That's not a number!"); 
       scan.next(); // this is important! 
      } 
      phaseThreeNum= scan.nextInt(); 
     } while (phaseThreeNum<= 0); 

       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!"); 
     } 
    } 
} 
+0

Wie würde ich das beheben und sicherstellen, dass mein Programm keine Zeichenfolge –

+0

übergebe ich Test auf meiner Seite funktioniert gut für mich, haben Sie Ihr Programm durch die Übergabe richtiger "Integer-Wert" zu testen für dein Programm. –

+0

Ja, ich habe. Ich habe dieses Programm viele Male auf Dr. Java ausgeführt und es funktioniert einwandfrei, wenn richtige Integer-Werte verwendet werden. Weißt du, wie ich die Weitergabe von Strings verhindern kann? –

Verwandte Themen