簡體   English   中英

我需要根據用戶輸入重復此 java 代碼,我無法使用我目前擁有的代碼使其重復

[英]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

我需要根據用戶輸入重復此 java 代碼,並且我無法使用我目前擁有的代碼使其重復。 除了掃描儀之外,我不應該使用任何其他進口產品並使用 class 主要產品。 這是因為我們使用https://repl.it/languages/java10作為我們的編譯器,因為我們是基本的 class。 當我運行代碼時,它應該詢問十個隨機加減法,並且應該詢問用戶是否要繼續。 當輸入 1 繼續時,它應該再問十個問題。 但是,在運行此代碼時,它會在第一個問題之后停止。

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

這就是為什么你提出這個問題System.out.println("Do you want ot continue? Put 1 for yes, 2 for no."); 出了while 我建議使用do while不是while 因此,您只需將問題放在循環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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM