【发布时间】:2014-03-05 03:42:49
【问题描述】:
我想知道是否有人能看出我的代码有什么问题。它可以工作,只是程序不承认我的 switch 语句 - 我搜索了很多问题,但由于我是新手,我显然遗漏了一些东西。
import java.util.Scanner;
class Calmlr1 {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
String anotherOption = "y", operatorOpt= "a";
int no1=0, no2=0;
double result= 0;
System.out.println ("Welcome to the online calculator! Let's begin...");
while (anotherOption.equalsIgnoreCase ("y")) {
System.out.println ("Please enter your 1st number: ");
no1 = input.nextInt();
System.out.println ("Please confirm your operator:\n1 = +\n2 = - \n3 = *\n4 = /");
operatorOpt = input.next ();
System.out.println ("Please enter your 2nd number: ");
no2 = input.nextInt();
switch(no1) {
case 1:
result=no1+no2;
break;
case 2:
result=no1-no2;
break;
case 3:
result=no1*no2;
break;
case 4:
result=no1/no2;
default:
result = 0 ;
break;
}
System.out.println("Your total calculation is: "+result);
System.out.println("Would you like to do another sum? y/n ");
anotherOption=input.next();
}
}
}
【问题讨论】:
-
没关系,我想通了 yaaaaaayyyy!!!! :) 需要将 operatorOpt 更改为 int 而不是 string 并且它可以工作:)
标签: java calculator