2017-12-27 55 views
-1

なぜ私は文字列として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(); 
    } 
} 
+3

'c'の型にマッチするために' char'リテラルを使います。 – rgettman

+1

あなたのケースでは 'char'を、スイッチでは' String'を使います。そのような単純な。 – shmosel

答えて

0

代わりにリテラル文字を使用することができます。

switch(c){ 
     case '+': 
      System.out.println(d1+d2); 
      break; 
... 

あなたがそれらを直接比較することはできませんので、種類が異なっています。この特定のケースでは、コンバーチブルになる可能性がありますが、それは一般的ではないので、コンパイラはそれを許すことはできません。

1

Stringcaseの式で使用できますが、charは不要です。 cのように変更

String c = var3.substring(0, 1); 

あなたのコードは動作します。または、ケース・ステートメントを変更してcharを使用してください。同様に、

switch (c) { 
case '+': 
    System.out.println(d1 + d2); 
    break; 
case '-': 
    System.out.println(d1 - d2); 
    break; 
case '*': 
    System.out.println(d1 * d2); 
    break; 
case '/': 
    System.out.println(d1/d2); 
    break; 
default: 
    System.out.println("Unrecognized operation"); 
    break; 
} 
0

またJDK v1.7デベロッパーからは、お使いのJavaのバージョンを確認し、コードの下に試してみると、それは代わりに文字列case "+":を比較すること

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 '+': 
      System.out.println(d1 + d2); 
      break; 
     case '-': 
      System.out.println(d1 - d2); 
      break; 
     case '*': 
      System.out.println(d1 * d2); 
      break; 
     case '/': 
      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(); 

} 

変更case '+':を実行しますが、あなたがスイッチ内の文字列を使用できるようになりますあなたがあなたのコードスニペットで行ったことと同じステートメント。

-1

文字列をcharに変換することはできません。文字列が大きくなるためです。 文字列が複数の文字で構成されているように考えると、文字列のようになります。

+0

これはどういう意味ですか、これは質問の回答(またはそれに関係するもの)ですか?明確にすることができれば、これはコメントかもしれません。しかし、それは質問に答えようとしないので、答えとして掲示すべきではありません。 –

+0

彼は尋ねた: "私は文字列をcharに変換できない理由"、それは誰も彼/彼女に言った部分ではなかった –

関連する問題