无法解决java中的错误,是否有任何修复?
这是我得到的错误,我似乎无法找出如何修复它。 我试图让它,一旦你完成你的计算,程序会问你,如果你想再次执行它们 线程“main”java.lang中出现异常。错误:未解决的编译问题: 无法解决重试问题 是无法解析为变量 我的代码(不要评判,这是我在java中的第一个程序): 我帮你修好了无法解决java中的错误,是否有任何修复?,java,eclipse,loops,Java,Eclipse,Loops,这是我得到的错误,我似乎无法找出如何修复它。 我试图让它,一旦你完成你的计算,程序会问你,如果你想再次执行它们 线程“main”java.lang中出现异常。错误:未解决的编译问题: 无法解决重试问题 是无法解析为变量 我的代码(不要评判,这是我在java中的第一个程序): 我帮你修好了 String retry; String again; do { Scanner num = new Scanner(System.in);
String retry;
String again;
do {
Scanner num = new Scanner(System.in);
System.out.println("Please enter the first number."+"\n");
int no1 = num.nextInt();
System.out.println("\n"+"The number you have entered is "+no1+"."+"\n"+"\n"+"Please enter the second number now."+"\n");
int no2 = num.nextInt();
System.out.println("\n"+"The number you have entered is "+no2+"."+"\n"+"\n"+"Please choose what you would like to do from the following options:"+"\n"+
"1)Addition"+"\n"+"2}Subtraction(1st Number-2nd Number)"+"\n"+"3)Subtraction(2nd Number-1st Number)"+"\n"+"4)Multiplication"+"\n"+"5)Division(1st Number divided by 2nd Number)"+"\n"+"6)Division(2nd Number divided by 1st Number)"
+ ""+"\n"+"7)Multiply by an exponent(1st Number)"+"\n"+"8)Multiply by an exponent(2nd Number)"+"\n"+"\n"+"Type any number from 1-8 to select your option."+"\n");
//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("\n"+"You have chosen "+choice +"\n");
switch (choice)
{
case 1:
float addition = no1+no2;
System.out.println("\n"+ addition);
break;
case 2:
float subtraction1 = no1-no2;
System.out.println("\n"+ subtraction1);
break;
case 3:
float subtraction2 = no2-no1;
System.out.println("\n"+ subtraction2);
break;
case 4:
float multiplication = no1*no2;
System.out.println("\n"+ multiplication);
break;
case 5:
double division1 = no1/no2;
System.out.println("\n"+ division1);
break;
case 6:
double division2 = no2/no1;
System.out.println("\n"+ division2);
break;
case 7:
System.out.println("\n"+ "Please enter the power."+"\n");
int exponent = num.nextInt();
double exponent1 = (int) Math.pow(no1, exponent);
System.out.println("\n"+ exponent1);
break;
case 8:
System.out.println("\n"+ "Please enter the power."+"\n");
int exponenttwo = num.nextInt();
double exponent2 = (int) Math.pow(no2, exponenttwo);
System.out.println("\n"+ exponent2);
break;
default:
System.out.println("\n"+ "There isnt any such option matching your entry!"+"\n");
break;
}
System.out.println("\n"+ "Would you like to perform more calculations? Respond with yes or no."+"\n");
retry = num.next();
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."+"\n");
int no1 = num.nextInt();
System.out.println("\n"+"The number you have entered is "+no1+"."+"\n"+"\n"+"Please enter the second number now."+"\n");
int no2 = num.nextInt();
System.out.println("\n"+"The number you have entered is "+no2+"."+"\n"+"\n"+"Please choose what you would like to do from the following options:"+"\n"+
"1)Addition"+"\n"+"2}Subtraction(1st Number-2nd Number)"+"\n"+"3)Subtraction(2nd Number-1st Number)"+"\n"+"4)Multiplication"+"\n"+"5)Division(1st Number divided by 2nd Number)"+"\n"+"6)Division(2nd Number divided by 1st Number)"
+ ""+"\n"+"7)Multiply by an exponent(1st Number)"+"\n"+"8)Multiply by an exponent(2nd Number)"+"\n"+"\n"+"Type any number from 1-8 to select your option."+"\n");
//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("\n"+"You have chosen "+choice +"\n");
switch (choice)
{
case 1:
float addition = no1+no2;
System.out.println("\n"+ addition);
break;
case 2:
float subtraction1 = no1-no2;
System.out.println("\n"+ subtraction1);
break;
case 3:
float subtraction2 = no2-no1;
System.out.println("\n"+ subtraction2);
break;
case 4:
float multiplication = no1*no2;
System.out.println("\n"+ multiplication);
break;
case 5:
double division1 = no1/no2;
System.out.println("\n"+ division1);
break;
case 6:
double division2 = no2/no1;
System.out.println("\n"+ division2);
break;
case 7:
System.out.println("\n"+ "Please enter the power."+"\n");
int exponent = num.nextInt();
double exponent1 = (int) Math.pow(no1, exponent);
System.out.println("\n"+ exponent1);
break;
case 8:
System.out.println("\n"+ "Please enter the power."+"\n");
int exponenttwo = num.nextInt();
double exponent2 = (int) Math.pow(no2, exponenttwo);
System.out.println("\n"+ exponent2);
break;
default:
System.out.println("\n"+ "There isnt any such option matching your entry!"+"\n");
break;
}
System.out.println("\n"+ "Would you like to perform more calculations? Respond with yes or no."+"\n");
retry = num.next();
again = "yes";
}while(retry.equalsIgnoreCase(again));
我回答这个问题只是为了进一步阐述努萨克的答案
Scanner num=new Scanner(System.in)时代码>在循环内,每次循环运行时都会创建一个新的扫描仪对象。因为我们总是从系统进行扫描。在中,我们可以使用相同的对象来扫描所有迭代
由于Java自己管理内存,这不是什么大问题,但在其他语言中可能会有问题
Scanner num=新的扫描仪(System.in);
再次String=“是”//您可以在这里再次初始化
字符串重试=”;//始终使用Java等语言进行初始化以避免错误。
做{
...
}
while((重试=num.next()).equalsIgnoreCase(再次));
//你也可以使用这个,也就是说,同时分配和评估,但它更难阅读。
我的回答只是为了进一步阐述努萨克的回答
您应该声明像Scanner这样的实用程序,它可以在for循环之外多次使用。
当您放置Scanner num=new Scanner(System.in)时代码>在循环内,每次循环运行时都会创建一个新的扫描仪对象。因为我们总是从系统进行扫描。在中,我们可以使用相同的对象来扫描所有迭代
由于Java自己管理内存,这不是什么大问题,但在其他语言中可能会有问题
Scanner num=新的扫描仪(System.in);
再次String=“是”//您可以在这里再次初始化
字符串重试=”;//始终使用Java等语言进行初始化以避免错误。
做{
...
}
while((重试=num.next()).equalsIgnoreCase(再次));
//你也可以使用这个,也就是说,同时分配和评估,但它更难阅读。
在块中声明重试
,因此在该块外不可用。在块中声明重试
,因此在该块外不可用。