2017-11-15 14 views
0
What is the result? 
10 - 7 = 
3 
Congratulations, you got it correct! 
Exception in thread "main" What is the result? 
9 + 8 = 
java.util.NoSuchElementException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at proj3.Project4App.main(Project4App.java:28) 

私のコードを実行すると、最初のループがスキャナで処理されていますが、エラーが発生します。私は何が間違っているのか分からないのですか? 10個の質問が正しく答えるまで、20個が全体的に答えられるまで、または正しい答えの平均が85%以上になるまでループを進める必要があります。スレッド "main"の例外java.util.scanner

package proj3; 
import java.util.Scanner; 
import java.util.Random; 

/** 
* <p> Title: Project 4: The Project4App Class </p> 
* <p> Description: Creates an math game for children </p> 
* @author Justin Abeles 
*/ 

public class Project4App { 
    /** 
    * <p> Name: main method </p> 
    * 
    * @param args values to be sent to the method 
    */ 
public static void main(String args[]){ 

    int addCorrect = 0; 
    int addWrong = 0; 
    int subCorrect = 0; 
    int subWrong = 0; 

    while(true){ 
     Question quiz = new Question(); 
     Scanner scan = new Scanner(System.in); 
     System.out.println("What is the result?\n" + quiz.toString()); 
     int age = scan.nextInt(); 
     int answer = quiz.determineAnswer(); 
     char operator = quiz.getOperator(); 
     if(age == answer && operator == '+') 
      System.out.println("Congratulations, you got it correct!"); 
     if((age == answer) && (operator == '-')) 
      System.out.println("Congratulations, you got it correct!"); 
     if((age != answer) && (operator == '+')) 
      System.out.println("The correct answer for " + quiz.toString() + " is " + quiz.determineAnswer()); 
     if((age != answer) && (operator == '-')) 
      System.out.println("The correct answer for " + quiz.toString() + " is " + quiz.determineAnswer()); 


     if(age == answer && operator == '+') 
      addCorrect = addCorrect + 1; 
     else if(age == answer && operator == '-') 
      subCorrect = subCorrect + 1; 
     else if(age != answer && operator == '+') 
      addWrong = addWrong + 1; 
     else 
      subWrong = subWrong + 1; 
     scan.close(); 
    } 
} 

}

答えて

0

は、あなたのサイクルのうちScanner scan = new Scanner(System.in);ます。 main機能を終了する前に、それをscan.close;と同じように実行してください。 closeScannerにすると、そのソースも閉じます。あなたの例外の理由かもしれません。

関連する問題