Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6915011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:25:17+00:00 2026-05-27T09:25:17+00:00

I made a Java program that did some basic math for kids in primary.

  • 0

I made a Java program that did some basic math for kids in primary. I wanted to make it a little more advanced, and remember the questions they got wrong, and then optionally present these questions again at the end and allow them to attempt them once more.

But the code for it got really complicated really quick. I thought I finished it, but it’s having difficulty compiling (read: won’t) and I’m at the point where I need some help. I’d greatly appreciate it.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:25:17+00:00Added an answer on May 27, 2026 at 9:25 am

    The errors are:

    1. You forgot to import java.util.List. Just go ahead and import java.util.*. You know you want to.

    2. You didn’t declare wrongIndexArray, array, wrong, wrongList, keyboard, a, or b anywhere. I may have missed a few.

    3. In this line:

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

    wrongList is an List<Integer> so I think that you can’t cast it to an int[]. You have to use Integer[].

    Possibly others, but…

    …based on the errors you have, are you using an IDE such as NetBeans? Using an IDE will go far in letting you identify the reasons for the errors you’re getting, and they often include helpful hints with each error.

    Some other helpful hints:

    It looks like you’re storing the operands in a list when the student gets the answer wrong. Consider instead wrapping the operands and the operation into a class such as SubtractionQuestion or AdditionQuestion. It might help later on when you want to retest the student on the ones they got wrong.

    Store the “yes” and “no” text answers (Yeah, Yes, Yea, Yup) in an ArrayList<String> yesAnswers, then just check if yesAnswers.contains(answer). This makes it easy to maintain the list, and you also don’t need to check against yes, no, or not yes or no — just check against yes, else no, else bad answer.

    Hope that makes sense….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a Java program that runs on a computer with two RS-232 ports.
I made a simple java program that sends bytes to the parallel port, which
I have a problem.... I made a java program that does the following: BufferedReader
I have made a java GUI program and have added a jList on that
I'm trying to make a bash script that will talk to a java program
I have made a program in Java that calculates powers of two, but it
K so last year I made a family tree program in java as a
I made a Java Applet with some Standard GUI Components on it. I used
I have made a java application that stores data from a .csv file to
In the Java Swing app I made it seems to me that all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.