2017-10-14 6 views
-1

Je n'arrive pas à comprendre ce qui ne va pas avec ce code. Sur Dr.Java tout fonctionne bien mais sur un autre code exécutant la plate-forme appelée edhesive (qui est où j'ai été affecté ce projet) il me donne une erreur. J'ai vérifié tout ce que je pouvais penser va mal, mais je ne sais toujours pas ce qui ne va pas.Erreur d'exécution - Exception dans le thread "principal" 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!"); 
      } 
    } 
} 

Après avoir exécuté sur edhesive, je reçois cette erreur

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) 

quelqu'un peut me aider s'il vous plaît?

+1

mettre un point d'arrêt sur la ligne 184 et Scanner.nextInt appeler, voir le résultat. –

+0

Voici quelques questions dans SO peut vous aider à comprendre cette exception: https://stackoverflow.com/questions/13408799/what-is-a-java-util-inputmismatchexception https://stackoverflow.com/questions/19460363/scanners -exception-java-util-inputmismatchexception – esprittn

Répondre

0

Run Time Error - Exception dans le thread « principal » java.util.InputMismatchException

scan.nextInt(); accepte uniquement des nombres entiers, vous pouvez passer String. Ainsi, il vous donne 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

Comment est-ce que je corrigerais cela et m'assurerais que mon programme ne passe pas une chaîne –

+0

Je fais des tests à mes côtés fonctionne bien pour moi, Avez-vous testé votre programme en passant la 'valeur entière 'appropriée pour votre programme. –

+0

Oui, j'ai. J'ai couru ce programme plusieurs fois sur Dr. Java et cela fonctionne parfaitement bien quand j'utilise des valeurs entières correctes. Savez-vous comment je peux empêcher les chaînes d'être passées? –