2017-10-14 1 views
-1

이 코드의 문제점을 이해하는 데 어려움이 있습니다. Dr.Java 에 모든 것이 잘 작동하지만 edhesive (이 프로젝트에 할당 된 곳)라는 다른 코드 실행 플랫폼에서 오류가 발생합니다. 내가 잘못 생각하고 있지만 여전히 잘못 알지 못하는 모든 것을 검사했습니다. edhesive에 그것을 실행 한 후런타임 오류 - 스레드 "main"의 예외 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!"); 
      } 
    } 
} 

,이 오류

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) 

누군가가 제발 도와 드릴거야?

+1

이 결과를 참조 Scanner.nextInt를 호출 라인 184에 중단 점을 넣어 준다. –

+0

여기에 몇 가지 질문이 있으므로이 예외를 이해하는 데 도움이 될 수 있습니다. https://stackoverflow.com/questions/13408799/what-is-a-java-util-inputmismatchexception https://stackoverflow.com/questions/19460363/scanners -exception-java-util-inputmismatchexception – esprittn

답변

0

실행 시간 오류 - 스레드의 예외 "주" java.util.InputMismatchException

scan.nextInt();에만 정수를 받아들이는, 당신은 문자열을 전달 할 수있다. 그래서, 그것은 당신 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

어떻게 수정하고 내 프로그램이 문자열을 전달하지 않는지 확인하십시오. –

+0

내 측에서는 테스트를 제대로 수행합니다. 적절한 '정수 값'을 전달하여 프로그램을 테스트 했습니까? 귀하의 프로그램에 대한. –

+0

예. 나는이 프로그램을 Dr. Java에서 여러 번 돌렸고 올바른 정수 값을 사용할 때 완벽하게 작동합니다. 문자열이 전달되는 것을 막을 수있는 방법을 알고 있습니까? –

관련 문제