这个Java程序有问题需要做一些数学运算

时间:2011-12-08 01:55:19

标签: java math

我制作了一个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.");
        }
    }
}

1 个答案:

答案 0 :(得分:3)

错误是:

  1. 您忘记导入java.util.List。只需继续导入java.util。*。你知道你想要的。

  2. 您没有在任何地方声明wrongIndexArray,array,wrong,wrongList,keyboard,a或b。我可能错过了一些。

  3. 在这一行:

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

  4. wrongList是List<Integer>所以我认为你不能将它转换为int []。你必须使用Integer []。

    可能是其他人,但是......

    ...基于您所遇到的错误,您使用的是NetBeans等IDE吗?使用IDE可以帮助您确定出现错误的原因,并且它们通常包含每个错误的有用提示。

    其他一些有用的提示:

    当学生得到错误答案时,您似乎将操作数存储在列表中。考虑将操作数和操作包装成一个类,例如SubtractionQuestion或AdditionQuestion。如果你想重新测试那些错误的学生,这可能会有所帮助。

    将“是”和“否”文本答案(是的,是的,是的,是的)存储在ArrayList&lt; String&gt;中。 yesAnswers,然后检查yesAnswers.contains(回答)。这样可以很容易地维护列表,而且你也不需要检查是,否,或者不是是或否 - 只需检查是,否则否,否则回答错误。

    希望有道理......