当前位置: 首页 > 知识库问答 >
问题:

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

乐正穆冉
2023-03-14

我为扫描器创建了一个方法:

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;
     }
   }
System.out.println("Residencey:" + state);

共有1个答案

东郭昌胤
2023-03-14

如果您的要求是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';
        }
    }
}

我希望它能帮助你:)

 类似资料: