java 关于不兼容类型的错误:字符串无法转换为字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36274582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:11:30  来源:igfitidea点击:

error about incompatible types: String cannot be converted to char

javaincompatibletypeerror

提问by Leo Lin

I create a method for Scanner:

我为 Scanner 创建了一个方法:

public static char getStatus() {
   Scanner kb = new Scanner(System.in);
   System.out.print("You are in state student or not(yes for Y/y,no for N/n)");
   char state = kb.nextLine().charAt(0);
   if (state == 'Y'||state == 'y') {
      String instate  = "In State";
      System.out.println(instate);
      int statusfee = 5;
      return instate;
      } else if (state == 'N'||state == 'n') {
      String outstate = "Out-of-State";
      System.out.println(outstate);
      int statusfee = 5+2;
      return outstate;
      } else {
      String false = "FALSE";
      System.out.print(false);
      return false;
     }
   }

I try to print out for my choice. For Example:

我试着打印出来供我选择。例如:

System.out.println("Residencey:" + state);

I want when I type "Y or y" then the output is Residencey: In State.

我希望当我输入“Y 或 y”时,输出是 Residencey: In State。

And the Errors are:

错误是:

error: incompatible types: String cannot be converted to char

错误:不兼容的类型:字符串无法转换为字符

I don't know why, does java cannot return String or sentence? I'm a beginner in java, So I cannot use some difficult method.

我不知道为什么,java不能返回字符串或句子吗?我是java初学者,所以我不能使用一些困难的方法。

回答by Alice

If your requirement is char status = getStatus();I think your Class look like:

如果你的要求是char status = getStatus();我认为你的班级看起来像:

public class ScannerTest {
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        char status = getStatus(kb);
        if (status == 'Y') {
            // System.out.println("In State");
        } else if (status == 'N') {

            // System.out.println("Out-of-State");
        } else {
            // System.out.println("FALSE");
        }
        System.out.println("Status is " + status);
    }

    public static char getStatus(Scanner kb) {

        System.out
                .print("You are in state student or not(yes for Y/y,no for N/n)");
        char state = kb.nextLine().charAt(0);
        if (state == 'Y' || state == 'y') {
            String instate = "In State";
            System.out.println(instate);
            int statusfee = 5;
            return 'Y';
        } else if (state == 'N' || state == 'n') {
            String outstate = "Out-of-State";
            System.out.println(outstate);
            int statusfee = 5 + 2;
            return 'N';
        } else {
            String myfalse = "FALSE";
            System.out.print(myfalse);
            return 'F';
        }
    }
}

I hope it will help you :)

我希望它会帮助你:)

回答by Ashu Kanaujia

your method return type is char and you are returning string that's why it is showing error . Here is your code with correct return type

您的方法返回类型是 char 并且您正在返回 string 这就是它显示 error 的原因。这是具有正确返回类型的代码

public static String getStatus () {
    Scanner kb = new Scanner (System.in);
    System.out.print ("You are in state student or not(yes for Y/y,no for N/n)");
    char state = kb.nextLine ().charAt (0);
    if ((state == 'Y') || (state == 'y')) {
        String instate = "In State";
        System.out.println (instate);
        int statusfee = 5;
        return instate;
    } else if ((state == 'N') || (state == 'n')) {
        String outstate = "Out-of-State";
        System.out.println (outstate);
        int statusfee = 5 + 2;
        return outstate;
    } else {
        String FALSE = "FALSE";
        System.out.print (false);
        return FALSE;
    }
}

回答by Alice

Please change your method return type to String. Example

请将您的方法返回类型更改为String. 例子

public class ScannerTest {
    public static void main(String[] args) {
        getStatus();
    }

    public static String getStatus() {
        Scanner kb = new Scanner(System.in);
        System.out.print("You are in state student or not(yes for Y/y,no for N/n)");
        char state = kb.nextLine().charAt(0);
        if (state == 'Y' || state == 'y') {
            String instate = "In State";
            System.out.println(instate);
            int statusfee = 5;
            return instate;
        } else if (state == 'N' || state == 'n') {
            String outstate = "Out-of-State";
            System.out.println(outstate);
            int statusfee = 5 + 2;
            return outstate;
        } else {
            String myfalse = "FALSE";
            System.out.print(myfalse);
            return myfalse;
        }
    }
}

I changed your variable name.

我改变了你的变量名。