[英]method that returns char with int
有人可以向我解释此方法吗,为什么返回char
而不是int
?
public static int fgetc(InputStream stream)
{
char ToRet = '\0';
if (InStream.isEmpty())
{
try
{
ToRet = (char) stream.read();
if (ToRet == CR)
Toret = (char) stream.read();
if ((int) ToRet == 0xFFFF)
return EOF;
}
catch (EOFException eof)
{
return EOF;
}
catch (IOException ioe)
{
writeline ("Unexpected IO Exception caught!\n", System.out);
writeline (ioe.toString(),System.out);
}
}
else
ToRet = ((MYLibCharacter) Instream.pop ()).charValue();
return ToRet;
}
并假设您要存储用户输入的字符值,直到检测到换行符为止,
int index = 0;
while (message[index] != '\n\)
{
message[index] = fgetc(System.in);
index++;
}
为什么这是错误的? 任何提示和帮助将不胜感激。
抱歉,这可能有点混乱,请随时编辑或询问有关此帖子的任何问题。
Java
中的char
(和其他原始类型)就像int
一样。
如果变量的类型为byte,short或char,并且常量表达式的值可表示为变量的类型,则可以使用缩窄的原始转换。
因此,就像我对上一个问题的上一个回答一样 ,您必须强制转换或更改返回类型,因为:
char c = 'A' + 1;
char c = 45;
要么
char c = 'A';
c = c++;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.