且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用户在Java中的基本阅读输入

更新时间:2023-12-04 18:02:52

您的代码期望使用char,但是您在此处返回int:

Your code is expecting a char, but you return an int here:

public static int fgetc(InputStream stream)
//            ↑ tells method will return an int

您可以

  • 更改方法签名以返回char.

public static char fgetc(InputStream stream)
//            ↑ tells method will return a char

  • 将值返回到char

    投射转换(§5.5)将表达式的类型转换为由强制转换运算符(

    Casting conversion (§5.5) converts the type of an expression to a type explicitly specified by a cast operator (§15.16).

    message[index] = (char) fgetc(System.in);
    //               ↑ cast returning value to a char