简体   繁体   中英

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 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);
  }
    }

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM