なぜ私は文字列としてswitch文&で使用する&を使用して文字列に変換しますか?私はそれがintまたはbyteまたはshortである必要があることを私に伝えることはできません!なぜ文字列をcharに変換してSwitch Statementで直接使用できないのですか?
public class Main {
public static void main(String[] args) {
String var1=getInput("enter first variable");
String var2=getInput("enter second variable");
String var3=getInput("enter opertaor");
char c = var3.charAt(0);
double d1=Double.parseDouble(var1);
double d2=Double.parseDouble(var2);
switch(c){
case "+"://squiggly line appears & the bubble help says incompatible types
System.out.println(d1+d2);
break;
case "-"://squiggly line appears & the bubble help says incompatible
System.out.println(d1-d2);
break;
case "*"://squiggly line appears & the bubble help says incompatible
System.out.println(d1*d2);
break;
case "/"://squiggly line appears & the bubble help says incompatible
System.out.println(d1/d2);
break;
default:
System.out.println("Unrecognized operation");
break;
}
}
static String getInput(String prompt){
System.out.println("prompt");
Scanner sc=new Scanner(System.in);
return sc.nextLine();
}
}
'c'の型にマッチするために' char'リテラルを使います。 – rgettman
あなたのケースでは 'char'を、スイッチでは' String'を使います。そのような単純な。 – shmosel