2011-12-08 17 views
0

私は主に子供のための基本的な計算を行ったJavaプログラムを作成しました。私はそれをもう少し進歩させ、間違った質問を覚えておき、必要に応じてこれらの質問を最後にもう一度提示して、もう一度試してみたいと思っていました。このJavaプログラムに何か問題がありました

しかし、そのコードは実際にはとても複雑です。私はそれを終えたと思ったが、それはコンパイルするのが難しいです(読まれません)、私は助けが必要なところにいます。私は大いに感謝しています。

import java.util.Scanner; 
import java.util.Random; 
public class ArithmeticTester0 { 
    public static void main(String[] arg) { 
     int level = 0; 
     String type = ""; 
     String name = "";  
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Hi! I am your friendly Math Tutor."); 
     System.out.print("What is your name? "); 
     name = keyboard.nextLine(); 
     System.out.print("Would you like to be tested on addition, subtraction or both? "); 
     type = keyboard.nextLine(); 

     // Check if the type inputted is anything other than addition, subtraction or both 
     if (!type.equalsIgnoreCase("addition") && !type.equalsIgnoreCase("subtraction") && !type.equalsIgnoreCase("both") && !type.equalsIgnoreCase("add") && !type.equalsIgnoreCase("subtraction")) { 
      while (!type.equalsIgnoreCase("addition") && !type.equalsIgnoreCase("subtraction") && !type.equalsIgnoreCase("both") && !type.equalsIgnoreCase("add") && !type.equalsIgnoreCase("subtraction")) { 
       System.out.print("You must answer addition, subtraction or both. How about we try again? "); 
       type = keyboard.nextLine(); 
      } 
     } 

     System.out.print("What level would you like to choose? " + 
      " Enter 1, 2 or 3: "); 
     level = keyboard.nextInt(); 

     // Check if the level entered is not 1, 2 or 3 
     if (level > 3 || level < 1) { 
      while (level > 3 || level < 1) { 
       System.out.println("The number must be either 1, 2 or" + 
        " 3. Let's try again shall we?"); 
       System.out.print("What level would you like to choose? "); 
       level = keyboard.nextInt(); 
      } 
     } 

     System.out.println("\nOK " + name + 
      ", here are 10 exercises for you at level " + level + "."); 
     System.out.println("Good luck!\n"); 

     int a = 0, b = 0, c = 0; 
     int preva = 0, prevb = 0; //previous a and b value 
     int correct = 0; 

     if (type.equalsIgnoreCase("addition") || type.equalsIgnoreCase("add")) { 
      add(level, preva, prevb, a, b, c, type); 
     } 
     else if (type.equalsIgnoreCase("subtraction") || type.equalsIgnoreCase("subtract")) { 
      subtract(level, preva, prevb, a, b, c, type); 
     } 
     else { 
      both(level, preva, prevb, a, b, c, type); 
     } 
    } 


    /* The method below prints out a statement depending 
    on how well the child did */ 

    public static void add(int level, int preva, int prevb, int a, int b, int c, String type) { 
     Random random = new Random(); 
     Scanner keyboard = new Scanner(System.in); 

     List<Integer> wrongList = Arrays.asList(array); 

     int correct = 0; 
     int wrong = 0; 

     // Generate 10 questions 
     for (int i = 1; i <= 10; i++) { 

      /* Generate numbers depending on the difficulty. 
      The while loop ensures that the next question isn't the same 
      as the previous question that was just asked. */ 
      if (level == 1) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(10); 
        b = random.nextInt(11-a); 
       } 
      } 
      else if (level == 2) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(10); 
        b = random.nextInt(10); 
       } 
      } 
      else if (level == 3) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(50); 
        b = random.nextInt(50); 
       } 
      } 

      preva = a; 
      prevb = b; 

      System.out.print(a + " + " + b + " = "); 
      c = keyboard.nextInt(); 

      // Check if the user was correct 

      if (a + b == c) { 
       System.out.println("You are right!"); 
       correct++; 
      } 
      if (a - b != c) { 
       wrongList.add(a); 
       wrongList.add(b); 
       wrong++; 
      } 
     } 

     // Conclusion 
     System.out.println("\nYou got " + correct + " right out of 10."); 
     if (wrong > 0) { 

      int[] wrongAnswers = wrongList.toArray(new int[wrongList.size()]); 

      System.out.print("You got " + wrong + " wrong, would you like to retry those questions?"); 
      String response = keyboard.nextLine(); 

      if (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea")) { 
       retry(wrongAnswers, type, wrongIndexArray); 
      } 
      else if (response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) { 
       System.out.println("OK! That's fine."); 
      } 
      else { 
       while (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea") || response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) { 
        System.out.println("Please put in an answer that is along the lines of \"yes\" or \"no\"."); 
        response = keyboard.nextLine(); 
       } 
      } 
     } 
     conclusion(correct, level); 
     System.out.println("See ya!"); 
    } 

    public static void subtract(int level, int preva, int prevb, int a, int b, int c, String type) { 
     Random random = new Random(); 
     Scanner keyboard = new Scanner(System.in); 

     List<Integer> wrongList = Arrays.asList(array); 

     // Generate 10 questions 
     int correct = 0; 
     int wrong = 0; 

     for (int i = 1; i <= 10; i++) { 

      /* Generate numbers depending on the difficulty. 
      The while loop ensures that the next question isn't the same 
      as the previous question that was just asked. */ 
      if (level == 1) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(10);  
        b = random.nextInt(11-a); 

        while (b > a) { 
         a = random.nextInt(10); 
         b = random.nextInt(11-a); 
        }  
       } 
      } 
      else if (level == 2) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(10); 
        b = random.nextInt(10); 

        while (b > a) { 
         a = random.nextInt(10); 
         b = random.nextInt(11-a); 
        } 
       } 
      } 
      else if (level == 3) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(50); 
        b = random.nextInt(50); 
       } 
      } 

      preva = a; 
      prevb = b; 

      System.out.print(a + " - " + b + " = "); 
      c = keyboard.nextInt(); 

      // Check if the user was correct 

      if (a - b == c) { 
       System.out.println("You are right!"); 
       correct++; 
      } 
      if (a - b != c) { 
       wrongList.add(a); 
       wrongList.add(b); 
       wrong++; 
      } 
     } 
     // Conclusion 
     System.out.println("\nYou got " + correct + " right out of 10."); 
     if (wrong > 0) { 

      int[] wrongAnswers = wrongList.toArray(new int[wrongList.size()]); 

      System.out.print("You got " + wrong + " wrong, would you like to retry those questions?"); 
      String response = keyboard.nextLine(); 

      if (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea")) { 
       retry(wrongAnswers, type); 
      } 
      else if (response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) { 
       System.out.println("OK! That's fine."); 
      } 
      else { 
       while (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea") || response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) { 
        System.out.println("Please put in an answer that is along the lines of \"yes\" or \"no\"."); 
        response = keyboard.nextLine(); 
       } 
      } 
     } 
     conclusion(correct, level); 
     System.out.println("See ya!"); 
    } 

    public static void both(int level, int preva, int prevb, int a, int b, int c, String type) { 
     Random random = new Random(); 
     Scanner keyboard = new Scanner(System.in); 

     // Generate 10 questions 
     int correct = 0; 

     List<Integer> wrongIndexes = Arrays.asList(array);  

     for (int i = 1; i <= 5; i++) { 

      /* Generate numbers depending on the difficulty. 
      The while loop ensures that the next question isn't the same 
      as the previous question that was just asked. */ 
      if (level == 1) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(10); 
        b = random.nextInt(11-a); 
       } 
      } 
      else if (level == 2) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(10); 
        b = random.nextInt(10); 
       } 
      } 
      else if (level == 3) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(50); 
        b = random.nextInt(50); 
       } 
      } 

      preva = a; 
      prevb = b; 

      System.out.print(a + " + " + b + " = "); 
      c = keyboard.nextInt(); 

      // Check if the user was correct 

      if (a + b == c) { 
       System.out.println("You are right!"); 
       correct++; 
      } 
      else { 
       System.out.println("Oops! You made a mistake. " + a + " + " + b + " = " + (a+b)); 
       wrongIndexes += i; 
      } 
     } 

     for (int i = 6; i <= 10; i++) { 

      /* Generate numbers depending on the difficulty. 
      The while loop ensures that the next question isn't the same 
      as the previous question that was just asked. */ 
      if (level == 1) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(10);  
        b = random.nextInt(11-a); 

        while (b > a) { 
         a = random.nextInt(10); 
         b = random.nextInt(11-a); 
        }  
       } 
      } 
      else if (level == 2) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(10); 
        b = random.nextInt(10); 

        while (b > a) { 
         a = random.nextInt(10); 
         b = random.nextInt(11-a); 
        } 
       } 
      } 
      else if (level == 3) { 
       while (preva == a && prevb == b) { 
        a = random.nextInt(50); 
        b = random.nextInt(50); 
       } 
      } 

      preva = a; 
      prevb = b; 

      System.out.print(a + " - " + b + " = "); 
      c = keyboard.nextInt(); 

      // Check if the user was correct 

      if (a - b == c) { 
       System.out.println("You are right!"); 
       correct++; 
      } 
      else { 
       System.out.println("Oops! You made a mistake. " + a + " - " + b + " = " + (a-b)); 
       wrongIndexes += i; 
      } 
     } 

     int[] wrongIndexArray = wrongIndexes.toArray(new int[wrongIndexes.size()]); 

     // Conclusion 
     System.out.println("\nYou got " + correct + " right out of 10."); 
     if (wrong > 0) { 

      int[] wrongAnswers = wrongList.toArray(new int[wrongList.size()]); 

      System.out.print("You got " + wrong + " wrong, would you like to retry those questions?"); 
      String response = keyboard.nextLine(); 

      if (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea")) { 
       retry(wrongAnswers, type, wrongIndexArray); 
      } 
      else if (response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) { 
       System.out.println("OK! That's fine."); 
      } 
      else { 
       while (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea") || response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) { 
        System.out.println("Please put in an answer that is along the lines of \"yes\" or \"no\"."); 
        response = keyboard.nextLine(); 
       } 
      } 
     } 
     conclusion(correct, level); 
     System.out.println("See ya!"); 
    } 

    public static void retry(int[] wrongAnswers, String type, int[] wrongIndexArray) { 
     int c = 0; 
     if (type.equalsIgnoreCase("addition") || type.equalsIgnoreCase("add")) { 
      for (int i = 0; i < wrongAnswers.length; i+=2) { 
       System.out.print(wrongAnswers[i] + " + " + wrongAnswers[i+1] + " = "); 
       c = keyboard.nextInt(); 

       if (wrongAnswers[i] + wrongAnswers[i+1] == c) { 
        System.out.println("You are right!"); 
       } 
       else { 
        System.out.println("Oops! You made a mistake. " + wrongIndex[i] + " + " + wrongIndex[i+1] + " = " + (wrongIndex[i]+wrongIndex[i+1])); 
       } 
      } 
     } 
     else if (type.equalsIgnoreCase("subtraction") || type.equalsIgnoreCase("subtract")) { 
      for (int i = 0; i < wrongAnswers.length; i+=2) { 
       System.out.print(wrongAnswers[i] + " - " + wrongAnswers[i+1] + " = "); 
       c = keyboard.nextInt(); 

       if (wrongAnswers[i] - wrongAnswers[i+1] == c) { 
        System.out.println("You are right!"); 
       } 
       else { 
        System.out.println("Oops! You made a mistake. " + a + " - " + b + " = " + (a-b)); 
       } 
      } 
     } 
     else { 
      for (int i = 0; i < wrongAnswers.length; i+=2) { 
       for (int i = 0; i < wrongIndexArray.length; i++) { 
        if (wrongIndexArray[i] < 6) { 
         System.out.print(wrongAnswers[i] + " + " + wrongAnswers[i+1] + " = "); 
         c = keyboard.nextInt(); 

         if (wrongAnswers[i] + wrongAnswers[i+1] == c) { 
          System.out.println("You are right!"); 
         } 
         else { 
          System.out.println("Oops! You made a mistake. " + wrongIndex[i] + " + " + wrongIndex[i+1] + " = " + (wrongIndex[i]+wrongIndex[i+1])); 
         } 
        } 
        else if (wrongIndexArray[i] > 5) { 
         System.out.print(wrongAnswers[i] + " - " + wrongAnswers[i+1] + " = "); 
         c = keyboard.nextInt(); 

         if (wrongAnswers[i] - wrongAnswers[i+1] == c) { 
          System.out.println("You are right!"); 
         } 
         else { 
          System.out.println("Oops! You made a mistake. " + wrongIndex[i] + " + " + wrongIndex[i+1] + " = " + (wrongIndex[i]+wrongIndex[i+1])); 
         } 
        } 
       }    
      } 
     } 
    } 

    public static void conclusion(int x, int lev) { 
     if (x >= 9) { 
      if (lev == 3) { 
       if (x == 9) 
        System.out.println("Please try the same difficulty again" + 
         ", you almost scored 10 out of 10!"); 
       else 
        System.out.println("You have mastered addition at this level" + 
         "! The system is not of any further use."); 
      } 
      else { 
       System.out.println("Please select a higher difficulty next time!"); 
      } 
     } 
     else if (x >= 6) { 
      System.out.println("Please try the test again."); 
     } 
     else { 
      System.out.println("Please ask your teacher for extra lessons."); 
     } 
    } 
} 
+4

エラーを投稿してください....すべてを読むことはありません。 – Max

+0

[SSCCE](http://sscce.org/)の投稿を検討してください。 – Jeffrey

+0

私はそれを読んで....何か間違って見てはいけない.... – jayunit100

答えて

3

エラーは以下のとおりです。

  1. あなたはjava.util.Listのをインポートするのを忘れました。単にjava.util。*をインポートしてください。自分がしたいって分かってるんでしょ。

  2. wrongIndexArray、array、wrong、wrongList、keyboard、a、またはbを宣言していませんでした。私はいくつか見逃しているかもしれません。この行で

  3. int[] wrongAnswers =wrongList.toArray(new int[wrongList.size()]);

wrongListので、私はあなたがint []にキャストすることはできませんを考えるList<Integer>です。 Integer []を使う必要があります。おそらく

他の人が...あなたが持っているエラーに基づいて

...、あなたは、NetBeansのようIDEを使用していますか? IDEを使用すると、取得したエラーの原因を特定できます。多くの場合、それぞれのエラーに役立つヒントが含まれています。

他のいくつかの有用なヒント:

あなたは生徒が間違った答えを取得し、リスト内のオペランドを保存しているように見えます。代わりに、オペランドと演算をSubtractionQuestionやAdditionQuestionなどのクラスにラップすることを検討してください。後であなたが間違っているものに学生を再テストしたい時に役立ちます。

ArrayListに「はい」と「いいえ」のテキスト回答を格納します。<文字列> yesAnswers、次にyesAnswers.contains(answer)をチェックします。これにより、リストの管理が容易になります。また、「はい」、「いいえ」、「はい」または「いいえ」をチェックする必要はありません。

希望しています。

+1

Robertと同じように、私は高度なIDEを使用することを強く推奨します。私は[Eclipse](http://www.eclipse.org/downloads/)を好むが、最終的にはすべて同じ機能を果たす。 EclipseとNetbeansは継続的にコンパイルし、エラーをチェックします。彼らはあなたのコードを修正するための優れた提案を提供します。 –

+1

そして、NetbeansとEclipseはどちらも無料です。 –

関連する問題