简体   繁体   English

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

[英]error about incompatible types: String cannot be converted to char

I create a method for 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:州。

And the Errors are: 错误是:

error: incompatible types: String cannot be converted to char 错误:类型不兼容:字符串无法转换为char

I don't know why, does java cannot return String or sentence? 我不知道为什么,java无法返回字符串或句子? I'm a beginner in java, So I cannot use some difficult method. 我是Java的初学者,所以我不能使用一些困难的方法。

If your requirement is char status = getStatus(); 如果您的要求是char status = getStatus(); I think your Class look like: 我认为您的班级看起来像:

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 :) 希望对您有所帮助:)

your method return type is char and you are returning string that's why it is showing error . 您的方法返回类型为char,并且您正在返回字符串,这就是为什么它显示error的原因。 Here is your code with correct return type 这是具有正确返回类型的代码

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

Please change your method return type to String . 请将您的方法返回类型更改为String Example

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. 我更改了您的变量名。

暂无
暂无

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

相关问题 错误:类型不兼容:char无法转换为String - error: incompatible types: char cannot be converted to String 错误:类型不兼容:对象无法转换为char - Error: incompatible types: Object cannot be converted to char 错误:不兼容的类型:String[] 无法转换为 String - error: incompatible types: String[] cannot be converted to String 错误:不兼容的类型:char[] 无法转换为 char (java) - Error: incompatible types: char[] cannot be converted to char (java) 错误:类型不兼容:字符串无法转换为URI - error: incompatible types: String cannot be converted to URI 错误:不兼容的类型:int 无法转换为 String - error: incompatible types: int cannot be converted to String 错误:不兼容的类型:无法将 Opiskelija 转换为字符串 - error: incompatible types: Opiskelija cannot be converted to String 错误:不兼容的类型:字符串无法转换为 int - error: incompatible types: String cannot be converted to int 错误:类型不兼容:字符串无法转换为char opp = JOptionPane.showInputDialog(“ Enter Method”); - error: incompatible types: String cannot be converted to char opp = JOptionPane.showInputDialog(“Enter Method”); 如何在我的股票投资组合中增加头寸,当前遇到有关不兼容类型的错误,无法将其转换为int或String - How to add position to my stock portfolio, currently getting an error about incompatible types cannot be converted to int or String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM