Search code examples
javaloopsfor-loopnested-loopsmultiplication

Multiplication Tutor Java Program


I am working on writing a program that follows these instructions:

Your little sister asks you to help her with her multiplication, and you decide to write a Java program that tests her skills. The program will let her input a starting number, such as 5. It will generate ten multiplication problems ranging from 5×1 to 5×10. For each problem she will be prompted to enter the correct answer. The program should check her answer and should not let her advance to the next question until the correct answer is given to the current question.

After testing ten multiplication problems, your program should ask whether she would like to try another starting number. If yes, your program should generate another corresponding ten multiplication problems. This procedure should repeat until she indicates no.

I have the code correct to ask for the multiplication part, but I can't quite figure out how to get the program to ask if the user wants to continue.

The following code has the program run through once:

    package hw5;
    import java.util.Scanner;
    public class HW5 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter number you would like to attempt: ");
            int start = input.nextInt();
            int mult;
            for (mult = 1; mult <= 10; mult++) {
                int num = start * mult;
                System.out.print(start + " x " + mult + " = ");
                int ans = input.nextInt();
                while (ans != num) {
                    System.out.print("Wrong answer, try again: ");
                    int ans2 = input.nextInt();
                    if (ans2 == num) {
                        break;
                    } 
                }

                //System.out.print("Would you like to do another problem? ");   

            }
        }
    }

When I uncomment out line 21 the program returns:

Enter number you would like to attempt: 1

1 x 1 = 1

Would you like to do another problem? 1 x 2 = 2

Would you like to do another problem? 1 x 3 =

etc...

If I take the code from line 21 and put it outside of the for loop the program runs the for loop once and then jumps straight to the question.

How do I go about fixing this and successfully completing the instructions?


Solution

  • Here's how I'd do it:

        package hw5;
        import java.util.Scanner;
        public class HW5 {
        public static void main(String[] args) 
        {
            boolean wantsToContinue = true;
            while(wantsToContinue)
            {
                wantsToContinue = mathProblem();
            }
        }
    
        public static boolean mathProblem()
        {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter number you would like to attempt: ");
            int start = input.nextInt();
            int mult;
            for (mult = 1; mult <= 10; mult++) {
                int num = start * mult;
                System.out.print(start + " x " + mult + " = ");
                int ans = input.nextInt();
                while (ans != num) {
                    System.out.print("Wrong answer, try again: ");
                    int ans2 = input.nextInt();
                    if (ans2 == num) {
                        break;
                    } 
                }
    
                //System.out.print("Would you like to do another problem? ");   
    
            }
    
            boolean wantsToContinue;
            //Ask if the user would like to do another problem here, set "wantsToContinue" accordingly
            return wantsToContinue;
        }
    }