Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在while循环条件下找不到符号错误_Java_Equals_Do While - Fatal编程技术网

Java 在while循环条件下找不到符号错误

Java 在while循环条件下找不到符号错误,java,equals,do-while,Java,Equals,Do While,我正在为一个任务编写一个程序,该任务应该会给用户随机的问题来解决。我试图让它做的是在选择一个问题类型并回答一个问题后,程序应该再次加载菜单 最初,我编写了一个方法,该方法将在第147行的else语句中调用。该方法已成功循环,但分配特别要求循环以使其发生。我尝试了几种不同的方法来更改循环条件语句,但我不确定哪里出错了?任何帮助都将不胜感激。 我非常想使用switch语句,但我不能,因为我们在课堂上还没有学会 // Importing Scanner and Random class for

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

最初,我编写了一个方法,该方法将在第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\u input
变量,然后可以在
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语句,或者将while语句替换为
while(true)

最后一个
while()
语句中的表达式不会编译,也永远不会为true,因为输入不能同时具有不同的值。我们不是在用量子计算机,你说得对。原来我有或者有。我想我需要在阴暗的时候停止编码。我真的很欣赏你的观点,非常感谢。当我写这篇文章时,我极度疲惫。你说的每句话都很有道理。
        while (!user_selection.equals("4"));