关于while循环和if语句嵌套

时间:2019-09-25 16:55:31

标签: java

我正在为我的第一个Java类解决名为“代数导师”的项目挑战。

  

程序应该能够输出一个问题,以确定是否要对公式m中的by = mx + b或y求解。

     

一旦其中一个被一个数字选中:“选择1来解决Y2来解决M3来解决B和4退出。”

     

它应该为您提供解决公式的输出。

我正在该项目的第3部分中工作:

  

更新程序,以便每个问题类型模式都会反复询问问题,直到用户连续获得3个正确答案。

     

如果在特定模式下尝试超过3个问题,则程序应提供有关如何解决此类问题的提示。

     

学生连续正确回答了3个问题后,应显示总分(正确回答的问题数除以尝试的问题总数),然后再次显示菜单。

因此,我对如何放置从0开始的计数器感到困惑,每次用户正确回答时,计数器将被添加1,如果计数器连续3退出或退出则退出如果用户答案不正确,则为0。

我将提供我的代码,让我感到困惑的是,如何在if语句内进行一个while循环,然后在计数时进行另一个while循环,这让我感到困惑,但是一旦您看到我的代码,您就会看到我在在谈论。

所以我知道我可以这样放置一个柜台:

int counter = 0;
while (counter < 4) 
      if statement here 
       counter ++; 
      else 
        counter = 0;
import java.util.Scanner;

class AlgebraTutor {

    public static void main(String[] arg) {
        double min_value = -100;
        double max_value = 100;

        double m_value = generate_random(max_value, min_value);
        double x_value = generate_random(max_value, min_value);
        double b_value = generate_random(max_value, min_value);
        double y_value = generate_random(max_value, min_value);
        Scanner user_input = new Scanner(System.in);
        int user_answer_int = 0;

        while ((user_answer_int < 4) && (user_answer_int >= 0)) {
            System.out.println("Select 1 to Solve for Y, 2 to Solve for M, 3 to Solve for B and 4 to quit. ");
            user_answer_int = user_input.nextInt();
            if (user_answer_int == 1) {
                check_answer_for_y(m_value, x_value, b_value);
            }
            else if (user_answer_int == 2) {
                check_answer_for_m(x_value, y_value, b_value);
            }
            else if (user_answer_int == 3) {
                check_answer_for_b(m_value, x_value, y_value);
            }
            else {
                System.out.println("You are done");
            }

        }
    }

    static void check_answer_for_m(double x_value, double y_value, double b_value) {
        System.out.println("Solve For M Problem ");
        System.out.println("Given: ");
        System.out.println("b = " + b_value);
        System.out.println("x = " + x_value);
        System.out.println("y = " + y_value);

        System.out.print("What is the value of m? ");
        Scanner user_input = new Scanner(System.in);
        String user_answer_m = "";
        user_answer_m = user_input.next();
        int user_answer_int = Integer.parseInt(user_answer_m);

        int correct_answer_m = (((int)y_value - (int)b_value) / (int)x_value);

        if (user_answer_int == correct_answer_m){
            System.out.println("You are correct!");
        } else {
            System.out.print("Sorry, that is incorrect. ");
            System.out.println("The answer is " + correct_answer_m);

        }

    }
    static void check_answer_for_b(double m_value, double x_value, double y_value) {
        System.out.println("Solve For B Problem ");
        System.out.println("Given: ");
        System.out.println("m = " + m_value);
        System.out.println("x = " + x_value);
        System.out.println("y = " + y_value);

        System.out.print("What is the value of b? ");
        Scanner user_input = new Scanner(System.in);
        String user_answer_b = "";
        user_answer_b = user_input.next();
        int user_answer_int = Integer.parseInt(user_answer_b);

        int correct_answer_b = ((int)y_value - ((int)m_value * (int)x_value));

        if (user_answer_int == correct_answer_b){
            System.out.println("You are correct!");
        } else {
            System.out.print("Sorry, that is incorrect. ");
            System.out.println("The answer is " + correct_answer_b);

        }
    }

    static void check_answer_for_y(double m_value, double x_value, double b_value) {
        System.out.println("Solve For Y Problem ");
        System.out.println("Given: ");
        System.out.println("m = " + m_value);
        System.out.println("x = " + x_value);
        System.out.println("b = " + b_value);

        System.out.print("What is the value of y? ");

        Scanner user_input = new Scanner(System.in);
        String user_answer_y = "";
        user_answer_y = user_input.next();
        int user_answer_int = Integer.parseInt(user_answer_y);

        int correct_answer_y = (int) m_value * (int) x_value + (int) b_value;

        if (user_answer_int == correct_answer_y) {
            System.out.println("You are correct!");
        } else {
            System.out.print("Sorry, that is incorrect. ");
            System.out.println("The answer is " + correct_answer_y);

        }
    }
    static int generate_random(double max_value, double min_value) {

        return (int) ((int) (Math.random() * ((max_value - min_value)+ 1)) + min_value);
    }
}

我在其中输入用户输入号码的while循环的地方 根据数字,如果if语句跳入 我可以在哪里放置计数器以开始并捕获所有if语句,具体取决于用户选择的号码,然后如果用户连续3个正确,则程序应回到主要问题。我可以在已有的循环中进行另一次while循环吗?还是应该将其放在每个if语句中?

1 个答案:

答案 0 :(得分:-1)

将每个循环都放在此处

else if (user_answer_int == 3) {
        check_answer_for_b(m_value, x_value, y_value);
    }

将主要功能更改为此。

     public static void main(String[] arg) {

    double min_value = -100;
    double max_value = 100;

    double m_value = generate_random(max_value, min_value);
    double x_value = generate_random(max_value, min_value);
    double b_value = generate_random(max_value, min_value);
    double y_value = generate_random(max_value, min_value);
    Scanner user_input = new Scanner(System.in);
    int user_answer_int = 0;

    while ((user_answer_int < 4) && (user_answer_int >= 0)) {
      System.out.println("Select 1 to Solve for Y, 2 to Solve for M, 3 to Solve for B and 4 to quit. ");
      user_answer_int = user_input.nextInt();
      int i = 0;
      if (user_answer_int == 1) {
        while (i < 3){
          if (check_answer_for_y(generate_random(max_value, min_value), generate_random(max_value, min_value), generate_random(max_value, min_value))) {
            i++;
          }
          else {
            i = 0;
          }
        }
      }
      else if (user_answer_int == 2) {
        while (i < 3){
          if (check_answer_for_m(generate_random(max_value, min_value), generate_random(max_value, min_value), generate_random(max_value, min_value))) {
            i++;
          }
          else {
            i = 0;
          }
        }
      }
      else if (user_answer_int == 3) {
        while (i < 3){
          if (check_answer_for_b(generate_random(max_value, min_value), generate_random(max_value, min_value), generate_random(max_value, min_value))) {
            i++;
          }
          else {
            i = 0;
          }
        }
      }
      else {
        System.out.println("You are done");
      }
    }
  }
  static boolean check_answer_for_m(double x_value, double y_value, double b_value) {
    System.out.println("Solve For M Problem ");
    System.out.println("Given: ");
    System.out.println("b = " + b_value);
    System.out.println("x = " + x_value);
    System.out.println("y = " + y_value);

    System.out.print("What is the value of m? ");
    Scanner user_input = new Scanner(System.in);
    String user_answer_m = "";
    user_answer_m = user_input.next();
    int user_answer_int = Integer.parseInt(user_answer_m);

    int correct_answer_m = (((int)y_value - (int)b_value) / (int)x_value);

    if (user_answer_int == correct_answer_m){
      System.out.println("You are correct!");
      return true;
    } else {
      System.out.print("Sorry, that is incorrect. ");
      System.out.println("The answer is " + correct_answer_m);
      return false;

    }

  }
  static boolean check_answer_for_b(double m_value, double x_value, double y_value) {
    System.out.println("Solve For B Problem ");
    System.out.println("Given: ");
    System.out.println("m = " + m_value);
    System.out.println("x = " + x_value);
    System.out.println("y = " + y_value);

    System.out.print("What is the value of b? ");
    Scanner user_input = new Scanner(System.in);
    String user_answer_b = "";
    user_answer_b = user_input.next();
    int user_answer_int = Integer.parseInt(user_answer_b);

    int correct_answer_b = ((int)y_value - ((int)m_value * (int)x_value));

    if (user_answer_int == correct_answer_b){
      System.out.println("You are correct!");
      return true;
    } else {
      System.out.print("Sorry, that is incorrect. ");
      System.out.println("The answer is " + correct_answer_b);
      return false;
    }
  }

  static boolean check_answer_for_y(double m_value, double x_value, double b_value) {
    System.out.println("Solve For Y Problem ");
    System.out.println("Given: ");
    System.out.println("m = " + m_value);
    System.out.println("x = " + x_value);
    System.out.println("b = " + b_value);

    System.out.print("What is the value of y? ");

    Scanner user_input = new Scanner(System.in);
    String user_answer_y = "";
    user_answer_y = user_input.next();
    int user_answer_int = Integer.parseInt(user_answer_y);

    int correct_answer_y = (int) m_value * (int) x_value + (int) b_value;

    if (user_answer_int == correct_answer_y) {
      System.out.println("You are correct!");
      return true;
    } else {
      System.out.print("Sorry, that is incorrect. ");
      System.out.println("The answer is " + correct_answer_y);
      return false;

    }
  }
  static int generate_random(double max_value, double min_value) {

    return (int) ((int) (Math.random() * ((max_value - min_value)+ 1)) + min_value);
  }