Java BufferedReader输入给出了意外的结果
当我运行以下代码并在提示输入时键入50时:Java BufferedReader输入给出了意外的结果,java,input,io,bufferedreader,Java,Input,Io,Bufferedreader,当我运行以下代码并在提示输入时键入50时: private static int nPeople; public static void main(String[] args) { nPeople = 0; System.out.println("Please enter the amount of people that will go onto the platform : "); BufferedReader keyboard = new Buffere
private static int nPeople;
public static void main(String[] args) {
nPeople = 0;
System.out.println("Please enter the amount of people that will go onto the platform : ");
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
try {
nPeople = keyboard.read();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(" The number of people entered --> " + nPeople);
}
}
我得到以下输出:
请输入进入站台的人数:50
输入-->53的人数
为什么我输入50时返回53?谢谢 因为“5”等于53(ascii码)
有关更多详细信息,请参阅和。,因为“5”等于53(ascii码) 有关更多详细信息,请参阅和。方法从输入中读取单个字符 因此,当您将
50
作为输入传递时,它只读取5
并将其转换为ASCII
等价物,即53
以将其存储在int
变量中
我认为这里需要一个方法,它读取一行文本
try {
nPeople = Integer.parseInt(keyboard.readLine());
} catch (IOException e) {
e.printStackTrace();
}
您需要Integer.parseInt
方法将字符串表示形式转换为Integer方法从输入中读取单个字符
因此,当您将50
作为输入传递时,它只读取5
并将其转换为ASCII
等价物,即53
以将其存储在int
变量中
我认为这里需要一个方法,它读取一行文本
try {
nPeople = Integer.parseInt(keyboard.readLine());
} catch (IOException e) {
e.printStackTrace();
}
您需要Integer.parseInt
方法将字符串表示形式转换为Integer
BufferedReader键盘=新的BufferedReader(新的InputStreamReader(System.in));
试一试{
nPeople=keyboard.read();
}捕获(IOE异常){
e、 printStackTrace();
}
上述代码将仅读取输入的第一个字符
它显示了那个字符的ASCII值
试用
npeople = Integer.parseInt(keyboard.readLine());
BufferedReader键盘=新的BufferedReader(新的InputStreamReader(System.in));
试一试{
nPeople=keyboard.read();
}捕获(IOE异常){
e、 printStackTrace();
}
上述代码将仅读取输入的第一个字符
它显示了那个字符的ASCII值
试用
npeople = Integer.parseInt(keyboard.readLine());
尝试这样做:
private static int nPeople;
public static void main(String[] args) {
nPeople = 0;
System.out.println("Please enter the amount of people that will go onto the platform : ");
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
try {
String input = reader.readLine();
nPeople = Integer.parseInt(input);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(" The number of people entered --> " + nPeople);
}
尝试这样做:
private static int nPeople;
public static void main(String[] args) {
nPeople = 0;
System.out.println("Please enter the amount of people that will go onto the platform : ");
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
try {
String input = reader.readLine();
nPeople = Integer.parseInt(input);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(" The number of people entered --> " + nPeople);
}
什么是keyboard
以及read()
方法到底做了什么?什么是keyboard
以及read()
方法到底做了什么?您不能将字符串分配给int。您不能将字符串分配给int。没有Integer方法。parseInteger(…)没有Integer方法。parseInteger(…)很好,谢谢。然而,我发现使用扫描仪要容易得多。扫描器与BufferedReader之间有任何输入?@Jatt。。好吧,如果您的唯一目的是读取输入并打印它,那么BufferedReader
是一个更好的选择。但是如果你想用读取的输入做一些解析
,那么当然要用扫描器
。我想是的,干杯。我以后需要使用实际的int值,所以Scanner似乎是最好的方法。无论如何,至少我知道将来如何使用BufferedReader:)@Jatt。。当然可以。你应该知道你所有的选择。为了成为一名更好的程序员,了解更多总是好的。祝你好运,享受编码:)工作,谢谢。然而,我发现使用扫描仪要容易得多。扫描器与BufferedReader之间有任何输入?@Jatt。。好吧,如果您的唯一目的是读取输入并打印它,那么BufferedReader
是一个更好的选择。但是如果你想用读取的输入做一些解析
,那么当然要用扫描器
。我想是的,干杯。我以后需要使用实际的int值,所以Scanner似乎是最好的方法。无论如何,至少我知道将来如何使用BufferedReader:)@Jatt。。当然可以。你应该知道你所有的选择。为了成为一名更好的程序员,了解更多总是好的。祝你好运,享受编码:)