java中无法解决的错误,是否有任何修复?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中无法解决的错误,是否有任何修复?相关的知识,希望对你有一定的参考价值。

这是我遇到的错误,我似乎无法弄清楚如何解决。我正在尝试这样做,以便一旦完成计算,程序就会询问您是否要再次执行。

线程“主”中的异常java.lang.Error:未解决的编译问题:重试无法解决是,无法解析为变量

我的代码(不要判断,这是我在Java中的第一个程序):

package calculatorpls;

import java.util.Scanner;

public class calc {

    public static void main(String[]Args)


    {

        do {

    Scanner num = new Scanner(System.in);

    System.out.println("Please enter the first number."+"
");

    int no1 = num.nextInt();

        System.out.println("
"+"The number you have entered is "+no1+"."+"
"+"
"+"Please enter the second number now."+"
");

    int no2 = num.nextInt();

        System.out.println("
"+"The number you have entered is "+no2+"."+"
"+"
"+"Please choose what you would like to do from the following options:"+"
"+
        "1)Addition"+"
"+"2}Subtraction(1st Number-2nd Number)"+"
"+"3)Subtraction(2nd Number-1st Number)"+"
"+"4)Multiplication"+"
"+"5)Division(1st Number divided by 2nd Number)"+"
"+"6)Division(2nd Number divided by 1st Number)"
                + ""+"
"+"7)Multiply by an exponent(1st Number)"+"
"+"8)Multiply by an exponent(2nd Number)"+"
"+"
"+"Type any number from 1-8 to select your option."+"
");

    //String Addition;
    //String Subtraction(1st Number-2nd Number);
    //String Subtraction(2nd Number-1st Number);    
    //String Multiplication;
    //String Division(1st Number divided by 2nd Number);
    //String Division(2nd Number divided by 1st Number);
    //String Multiply by an exponent(1st Number);
    //String Multiply by an exponent(2nd Number);

        int choice = num.nextInt();

                System.out.println("
"+"You have chosen "+choice +"
");


        switch (choice)
        {       
        case 1: 

            float addition = no1+no2;
            System.out.println("
"+ addition);
            break; 

        case 2: 

            float subtraction1 = no1-no2;
            System.out.println("
"+ subtraction1);
            break; 

        case 3: 

            float subtraction2 = no2-no1;
            System.out.println("
"+ subtraction2);
            break; 

        case 4: 

            float multiplication = no1*no2;
            System.out.println("
"+ multiplication);
            break;

        case 5: 

            double division1 = no1/no2;
            System.out.println("
"+ division1);
            break;

        case 6: 

            double division2 = no2/no1;
            System.out.println("
"+ division2);
            break; 

        case 7: 

        System.out.println("
"+ "Please enter the power."+"
");
            int exponent = num.nextInt();
            double exponent1 = (int) Math.pow(no1, exponent);
            System.out.println("
"+ exponent1);
            break;

       case 8: 

           System.out.println("
"+ "Please enter the power."+"
");
            int exponenttwo = num.nextInt();
            double exponent2 = (int) Math.pow(no2, exponenttwo);
            System.out.println("
"+ exponent2);
            break;

        default: 

            System.out.println("
"+ "There isnt any such option matching your entry!"+"
");


            break; 

        }

        System.out.println("
"+ "Would you like to perform more calculations? Respond with yes or no."+"
");

        String retry = num.nextLine();
         String again = "yes";

    }while(retry.equalsIgnoreCase(again));

    }

}
答案

我为您修复了它。

         String retry;
         String again;
     do {

         Scanner num = new Scanner(System.in);

         System.out.println("Please enter the first number."+"
");

         int no1 = num.nextInt();

             System.out.println("
"+"The number you have entered is "+no1+"."+"
"+"
"+"Please enter the second number now."+"
");

         int no2 = num.nextInt();

             System.out.println("
"+"The number you have entered is "+no2+"."+"
"+"
"+"Please choose what you would like to do from the following options:"+"
"+
             "1)Addition"+"
"+"2}Subtraction(1st Number-2nd Number)"+"
"+"3)Subtraction(2nd Number-1st Number)"+"
"+"4)Multiplication"+"
"+"5)Division(1st Number divided by 2nd Number)"+"
"+"6)Division(2nd Number divided by 1st Number)"
                     + ""+"
"+"7)Multiply by an exponent(1st Number)"+"
"+"8)Multiply by an exponent(2nd Number)"+"
"+"
"+"Type any number from 1-8 to select your option."+"
");

         //String Addition;
         //String Subtraction(1st Number-2nd Number);
         //String Subtraction(2nd Number-1st Number);    
         //String Multiplication;
         //String Division(1st Number divided by 2nd Number);
         //String Division(2nd Number divided by 1st Number);
         //String Multiply by an exponent(1st Number);
         //String Multiply by an exponent(2nd Number);

             int choice = num.nextInt();

                     System.out.println("
"+"You have chosen "+choice +"
");


             switch (choice)
             {       
             case 1: 

                 float addition = no1+no2;
                 System.out.println("
"+ addition);
                 break; 

             case 2: 

                 float subtraction1 = no1-no2;
                 System.out.println("
"+ subtraction1);
                 break; 

             case 3: 

                 float subtraction2 = no2-no1;
                 System.out.println("
"+ subtraction2);
                 break; 

             case 4: 

                 float multiplication = no1*no2;
                 System.out.println("
"+ multiplication);
                 break;

             case 5: 

                 double division1 = no1/no2;
                 System.out.println("
"+ division1);
                 break;

             case 6: 

                 double division2 = no2/no1;
                 System.out.println("
"+ division2);
                 break; 

             case 7: 

             System.out.println("
"+ "Please enter the power."+"
");
                 int exponent = num.nextInt();
                 double exponent1 = (int) Math.pow(no1, exponent);
                 System.out.println("
"+ exponent1);
                 break;

            case 8: 

                System.out.println("
"+ "Please enter the power."+"
");
                 int exponenttwo = num.nextInt();
                 double exponent2 = (int) Math.pow(no2, exponenttwo);
                 System.out.println("
"+ exponent2);
                 break;

             default: 

                 System.out.println("
"+ "There isnt any such option matching your entry!"+"
");


                 break; 

             }

             System.out.println("
"+ "Would you like to perform more calculations? Respond with yes or no."+"
");

             retry = num.next();
             again = "yes";

         }while(retry.equalsIgnoreCase(again));
另一答案

我只是为了扩展nusaK的答案。

  1. 您应该声明诸如Scanner之类的实用程序,这些实用程序可以在for循环之外多次使用。

[将Scanner num = new Scanner(System.in);放入循环中时,每次循环运行都会创建一个新的扫描仪对象。由于我们始终从System.in进行扫描,因此我们可以使用同一对象扫描所有迭代。

由于Java自己管理内存,所以这不是什么大问题,但是可能是其他语言。

Scanner num = new Scanner(System.in);

String again = "yes"; // you can initialize again here 
String retry=""; // always initialize in languages like Java to avoid errors.

do {
   ...
}
while((retry=num.next()).equalsIgnoreCase(again));
// u can also use this, i.e. assign and evaluate at the same time but it's harder to read.

以上是关于java中无法解决的错误,是否有任何修复?的主要内容,如果未能解决你的问题,请参考以下文章

React 的登录重定向错误是不是有任何修复?

无法修复片段 NullPointerException 中的地图片段

片段 getActivity 不起作用

如何修复“代码应明确检查权限是不是可用”错误

如何修复解析器错误消息:无法加载类型?

我无法从 firebase 获取下载网址()。请任何人帮助这是我的代码和错误。 (我正在使用片段)[重复]