0

I need to make this java code repeat based on the user input, and I cannot make it repeat using the code that I do have so far. I am not supposed to use any other imports besides the scanner and use class main. This is because we are using the https://repl.it/languages/java10 as our compiler because we are an elementary class. When I run the code, it is supposed to ask ten random addition and subtraction and should ask if the user wants to continue or not. When entering 1 for continue, it should ask another ten questions. however, upon running this code, it stops after the first question.

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    System.out.println("Answer the following questions.");
    Scanner in = new Scanner(System.in);
    int A = 0;
    int N = 10;
    int n = 0;
    int H = 0;
    boolean p = true;
        while (p){
        int R1 = (int)(Math.random() * 50 + 1);
        int R2 = (int)(Math.random() * 999 + 1);
        int R3 = (int)(Math.random() * 999 + 1);
          if(R1>25){
          System.out.println( "" + R2 + " + " + R3);
          A = in.nextInt();
            if (A == (R2 + R3))
              System.out.println("Correct");
            else 
              System.out.println("Incorrect");
        }
          if(R1<25){
            System.out.println( "" + R2 + " - " +  R3);
           A = in.nextInt();
            if (A == (R2 - R3))
              System.out.println("Correct");
            else 
              System.out.println("Incorrect");}
        N--;
        if (N==0)
        p = false;
        continue;
        }System.out.println("Do you want ot continue? Put 1 for yes, 2 for no.");
        H = in.nextInt();
        if (H==1)
        p=true;
        else
        p=false;
      while (N>0);
  }
    }
Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Max Vollmer Sep 25 '19 at 17:57
  • What are you doing with this `while (N>0);` statement? Please fix the indent of your code, it will be easier to see mistakes in your code. – Progman Sep 25 '19 at 17:58
  • Please give your variables proper names. It is unnecessary hard to read and understand your code the way it's written. If a variable has a specific purpose then call it `specificPurposeFullfiller` not `A`. – Max Vollmer Sep 25 '19 at 17:59
  • You should really be giving your variables descriptive names. I suggest writing your problem in pseudo code and then programming based on that. Can you post back with pseudo code you made, if you do I'd be willing to help more. – ShaneDemskie Sep 25 '19 at 18:06

1 Answers1

1

That's why you put the question System.out.println("Do you want ot continue? Put 1 for yes, 2 for no."); out of the while. I recommend use do while instead of while. So you just need to put the question inside of loop do while.

        System.out.println("Answer the following questions.");
        Scanner in = new Scanner(System.in);
        int A = 0;
        int N = 10;
        int H = 0;
        boolean p = true;
        do{
            int R1 = (int) (Math.random() * 50 + 1);
            int R2 = (int) (Math.random() * 999 + 1);
            int R3 = (int) (Math.random() * 999 + 1);
            if (R1 > 25) {
                System.out.println("" + R2 + " + " + R3);
                A = in.nextInt();
                if (A == (R2 + R3)) {
                    System.out.println("Correct");
                } else {
                    System.out.println("Incorrect");
                }
            }
            if (R1 < 25) {
                System.out.println("" + R2 + " - " + R3);
                A = in.nextInt();
                if (A == (R2 - R3)) {
                    System.out.println("Correct");
                } else {
                    System.out.println("Incorrect");
                }
            }
            N--;

            System.out.println("Do you want ot continue? Put 1 for yes, 2 for no.");
            H = in.nextInt();
            if (H == 1) {
                p = true;
            } else {
                p = false;
            }
            if (N == 0) {
                p = false;
                System.out.println("You have reached your max attempts.")
            }
        }while (N > 0 && p);
Hasho
  • 79
  • 1
  • 6
  • And like the guys above, you need to put clear and descriptive names to your variables. It is highly recommended since after doing a lot of code, it becomes confusing to have many variables with meaningless names. – Hasho Sep 25 '19 at 18:10