简体   繁体   English

为什么我不能将 String 转换为 char 并直接在 Switch 语句中使用?

[英]why cant i convert String to char & use in Switch Statement directly?

why cant i convert String to char & use in Switch Statement & if i left it as string the switch statement wont accept it either telling me it needs to be int or byte or short !!为什么我不能将 String 转换为 char 并在 Switch 语句中使用 & 如果我将其保留为字符串,switch 语句将不会接受它,要么告诉我它需要是 int 或 byte 或 short !

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();
    }
}

You can use a String in case expressions, no need for a char .您可以在case表达式中使用String ,不需要char Change c like改变c喜欢

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

and your code would work.你的代码会起作用。 Alternatively, modify your case statements to use char .或者,修改您的 case 语句以使用char Like,喜欢,

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;
}

You can use a char literal instead:您可以改用字符文字:

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

The types are different so you can't directly compare them.类型不同,因此您无法直接比较它们。 They happen to be convertible in this particular case, but that's not true in general, so the compiler can't allow that.在这种特殊情况下,它们碰巧是可转换的,但通常情况下并非如此,因此编译器不允许这样做。

Try out below code and it will execute试试下面的代码,它会执行

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();

}

Change case '+': instead of comparing String case "+": Also Check your Java version, From JDK v1.7 will allow you to use Strings in switch statement as what you did in your code snippet.更改case '+':而不是比较 String case "+": Also Check your Java version, From JDK v1.7 将允许您在 switch 语句中使用字符串,就像您在代码片段中所做的那样。 Let me know if you are looking for another solution如果您正在寻找其他解决方案,请告诉我

You cant parse a String into a char because a String is to big to fit.您不能将字符串解析为字符,因为字符串太大而无法容纳。 Think of it like a String consist of multiple chars, so its just like a char array in one piece.把它想象成一个由多个字符组成的字符串,所以它就像一个字符数组一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM