2011-12-08 3 views
0

기본에있는 어린이들을위한 몇 가지 기본 수학을 수행 한 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는 List<Integer> 그래서 난 당신의 int []에 캐스팅 할 수없는 생각 입니다. Integer []를 사용해야합니다. 아마도

다른,하지만 ... 당신이 가지고있는 오류를 기반으로

..., 당신은 넷빈즈와 같은 IDE를 사용하고 있습니까? IDE를 사용하면 오류의 원인을 파악할 수 있으며 각 오류와 관련하여 유용한 힌트가 포함되는 경우가 많습니다.

다른 어떤 도움이되는 힌트 :

당신이 학생이 답변을 잘못 가져옵니다 목록에서 피연산자를 저장하는 것처럼 보인다. 대신 피연산자와 연산을 SubtractionQuestion 또는 AdditionQuestion과 같은 클래스로 래핑하는 것을 고려하십시오. 나중에 잘못한 학생들에게 재시험을 원할 때 도움이 될 수 있습니다.

스토어 "예"와 ArrayList를 < 문자열 > yesAnswers에서 "아니오"텍스트 답변 (네, 네, 네, 그렇습니다), 그럼 그냥 확인 yesAnswers.contains 경우 (답). 이렇게하면 목록을 쉽게 관리 할 수 ​​있으며 예, 아니요 또는 아니요 또는 아니요에 대해 확인할 필요가 없습니다. 예, 아니요, 그렇지 않으면 나쁜 대답을 확인하십시오.

희망이 맞는 것 ....

+1

Robert와 마찬가지로 상위 수준의 IDE를 사용하는 것이 좋습니다. 나는 [Eclipse] (http://www.eclipse.org/downloads/)를 선호하지만, 결국 그들은 모두 같은 기능을한다. Eclipse와 Netbeans은 지속적으로 컴파일하여 오류를 확인합니다.심지어 코드 수정에 대한 훌륭한 제안을 제공하기도합니다. –

+1

그리고 Netbeans과 Eclipse는 모두 무료입니다. –

관련 문제