java - while 循环条件下找不到符号错误

标签 java equals do-while

我正在为一项作业编写一个程序,该程序应该给出随机问题供用户解决。我试图让它做的是在选择问题类型并回答一个问题后,程序应该再次加载菜单。

最初,我编写了一个将在第 147 行的 else 语句中调用的方法。该方法成功循环,但赋值特别要求循环来实现它。我尝试了几种不同的方法来更改循环条件语句,但我不确定我哪里出错了?任何帮助,将不胜感激。 我非常想使用 switch 语句,但我不能,因为我们在类里面还没有学到这一点。

    // Importing Scanner and Random class for later.
import java.util.Scanner;
import java.util.Random;

class AlgebraTutor {
// Solve for Y method.
    public static void solve_for_y() {
// Creation of a random number generator.
        Random number_gen = new Random();

// Generates random integers from -100 to 100.
        int var_m = number_gen.nextInt(101) - 100;
        int var_x = number_gen.nextInt(101) - 100;
        int var_b = number_gen.nextInt(101) - 100;

// Print problem out for student to see
        System.out.println("Problem: y = " + var_m + "(" + var_x +")" + "+" + var_b);

        System.out.println(" m =" + var_m);
        System.out.println(" x =" + var_x); 
        System.out.println(" b =" + var_b); 

// This formula will calculate the value of y.        
         float var_y = (var_m * var_x) + var_b;

// Using the scanners class a scanner object called userInput was created to record students answer. Answer was taken as a string and converted to an integer.

     Scanner user_input = new Scanner(System.in);
     System.out.println("Please solve for y:");
     String user_answer = user_input.nextLine();

        int answer = Integer.parseInt(user_answer);  

         if (answer == var_y){
             System.out.println("correct");

         }else{
                  System.out.println("incorrect, The answer is:" + var_y);

                 }
                                            }      
// -------------------------------------------------------------------------


// Solve for M method.
     public static void solve_for_m() {
// Creation of a random number generator.
        Random number_gen = new Random();

// Generates random integers from -100 to 100.
        int var_y = number_gen.nextInt(101) - 100;
        int var_x = number_gen.nextInt(101) - 100;
        int var_b = number_gen.nextInt(101) - 100;

// Print problem out for student to see.
        System.out.println("Problem: " + var_y + " = m (" + var_x +") + " + var_b);

        System.out.println(" y =" + var_y);
        System.out.println(" x =" + var_x); 
        System.out.println(" b =" + var_b); 

// This formula will calculate the value of m.        
        float var_m = (var_y - var_b) / var_x;

// Using the scanners class a scanner object called userInput was created to record students answer. Answer was taken as a string and converted to an integer.

     Scanner user_input = new Scanner(System.in);
     System.out.println("Please solve for m:");
     String user_answer = user_input.nextLine();

        int answer = Integer.parseInt(user_answer);  

         if (answer == var_m){
             System.out.println("correct");

         }else{
                  System.out.println("incorrect, The answer is:" + var_m);

                 }
                                            }      

// -------------------------------------------------------------------------     

// Solve for B method 

     public static void solve_for_b() {
// Creation of a random number generator.
        Random number_gen = new Random();

// Generates random integers from -100 to 100.
        int var_y = number_gen.nextInt(101) - 100;
        int var_x = number_gen.nextInt(101) - 100;
        int var_m = number_gen.nextInt(101) - 100;

// Print problem out for student to see.
        System.out.println("Problem: " + var_y + " = " + var_m + " (" + var_x +") " + "+ b");

        System.out.println(" y =" + var_y);
        System.out.println(" x =" + var_x); 
        System.out.println(" m =" + var_m); 

// This formula will calculate the value of m.        
        float var_b = var_y / (var_m * var_x);

// Using the scanners class a scanner object called userInput was created to record students answer. Answer was taken as a string and converted to an integer.

     Scanner user_input = new Scanner(System.in);
     System.out.println("Please solve for b:");
     String user_answer = user_input.nextLine();

        int answer = Integer.parseInt(user_answer);  

         if (answer == var_b){
             System.out.println("correct");

         }else{
                  System.out.println("incorrect, The answer is:" + var_b);
                                   }
                                            }          


// -------------------------------------------------------------------------     
    public static void main(String[] args) {

        do{
        System.out.println("Which type of problem would you like to practice?");
        System.out.println("1) Solve for y");
        System.out.println("2) Solve for m"); 
        System.out.println("3) Solve for b");
        System.out.println("4) To quit");

        Scanner selection_input = new Scanner(System.in);
        String user_selection = selection_input.nextLine();

        if ( user_selection.equals("1")){
            solve_for_y();
        } else if (user_selection.equals("2")){
            solve_for_m();
        } else if (user_selection.equals("3")){
            solve_for_b();
        } else if (user_selection.equals("4")){
            System.out.println("Quitting Program");
            System. exit(0);
        } else{
               System.out.println("Please choose from the given options");
        }

        } while(user_selection.equals("1") &&
                user_selection.equals("2") &&
                user_selection.equals("3") &&
                user_selection.equals("4"));

    }

                   }

最佳答案

您必须在 do...while 循环之外声明 user_inpout 变量,然后您可以在 while() 中检查其值表达。另外,您应该在程序开始时仅初始化扫描仪一次。

    public static void main(String[] args)
    {

        Scanner selection_input = new Scanner(System.in);
        String user_selection=null;
        do
        {
            System.out.println("Which type of problem would you like to practice?");
            System.out.println("1) Solve for y");
            System.out.println("2) Solve for m");
            System.out.println("3) Solve for b");
            System.out.println("4) To quit");

            user_selection = selection_input.nextLine();

            if (user_selection.equals("1"))
            {
                solve_for_y();
            }
            else if (user_selection.equals("2"))
            {
                solve_for_m();
            }
            else if (user_selection.equals("3"))
            {
                solve_for_b();
            }
            else if (user_selection.equals("4"))
            {
                System.out.println("Quitting Program");
                System.exit(0);
            }
            else
            {
                System.out.println("Please choose from the given options");
            }

        }
        while (!user_selection.equals("4"));
    }

对于情况“4”,您现在有两个存在:

            else if (user_selection.equals("4"))
            {
                System.out.println("Quitting Program");
                System.exit(0);
            }

和:

        while (!user_selection.equals("4"));

两者仅需其中之一。因此,您可以删除第一个或将 while 语句替换为 while(true)

关于java - while 循环条件下找不到符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60247063/

相关文章:

java - 更改函数原型(prototype)会生成 "cannot be resolved. It is indirectly referenced"错误

java - 'equals()' 返回 false,但在 map 中找到了对象

css - 侧边栏的高度等于主要内容

java - Yaml文件读取对象歧义

java - Do-while 语句 Java

java - Canvas 中的网格,最后一个单元格的大小与其他单元格不同

java - 对EJB的定义感到困惑

java - Do-while 循环不会逃脱 - Java

Java....为输入中的每个字符打印 1 个错误

java - 我在循环时程序跳过第一个输入时遇到问题。请帮忙