returns char with int 的方法
method that returns char with int
谁能给我解释一下这个方法,为什么返回 char
而不是 int
??
public static int fgetc(InputStream stream)
{
char ToRet = '[=11=]';
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++;
}
为什么这是错误的??
任何提示和帮助将不胜感激。
抱歉,这可能有点混乱,请随时编辑或询问我有关此的任何问题 post。
char
(和其他基本类型)在 Java
中类似于 int
。
A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
因此,与 my previous answer to your previous question 中一样,您必须转换它或更改 return 类型,因为:
char c = 'A' + 1;
char c = 45;
或
char c = 'A';
c = c++;
谁能给我解释一下这个方法,为什么返回 char
而不是 int
??
public static int fgetc(InputStream stream)
{
char ToRet = '[=11=]';
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++;
}
为什么这是错误的?? 任何提示和帮助将不胜感激。
抱歉,这可能有点混乱,请随时编辑或询问我有关此的任何问题 post。
char
(和其他基本类型)在 Java
中类似于 int
。
A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
因此,与 my previous answer to your previous question 中一样,您必须转换它或更改 return 类型,因为:
char c = 'A' + 1;
char c = 45;
或
char c = 'A';
c = c++;