这是我正在尝试的基本程序
public class keyboardinput {
public static void main(String[] args) throws java.io.IOException {
int a ;
System.out.println ("enter the text");
a = (int) System.in.read();
System.out.println ("the entered value is :"+a );
}
}
执行后,显示如下响应
输入文字
1
输入的值为:49
我输入1,为什么不显示输入的值为1
你能告诉我为什么输出显示等效的 asci 值而不是我在输入中输入的值吗
最佳答案
你不能像你在这里做的那样只是从一个字节转换为一个整数:
a = (int) System.in.read();
System.in.read()
返回一个整数,但结果将是字符 1
的 ASCII 码,即 49。
我建议使用扫描仪:
Scanner s = new Scanner(System.in);
a = s.nextInt();
关于java - 基本代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34018743/