Java 基本代码错误

Java 基本代码错误,java,Java,这是我正在尝试的基本程序 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

这是我正在尝试的基本程序

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 );
    }
}
执行时,它将显示以下响应

输入文本

一,

输入的值是:49

当我输入1时,为什么不显示输入的值为1

您能告诉我为什么输出显示的是等效的asci值,而不是我在输入中输入的值吗?

49是符号1的ASCII码,您明确表示为int。要读取int值,请使用以下方法:

    try (BufferedReader bf = new BufferedReader(new InputStreamReader(System.in))) {
        a = Integer.parseInt(bf.readLine());
        System.out.println("the entered value is :" + a);
    }
49是符号1的ASCII码,显式表示int。要读取int值,请使用以下内容:

    try (BufferedReader bf = new BufferedReader(new InputStreamReader(System.in))) {
        a = Integer.parseInt(bf.readLine());
        System.out.println("the entered value is :" + a);
    }

您不能像这里所做的那样,将字节强制转换为整数:

a =  (int) System.in.read();
System.in.read返回一个整数,但结果将是字符1的ASCII码,即49

我建议使用扫描仪:

Scanner s = new Scanner(System.in);
a = s.nextInt();

您不能像这里所做的那样,将字节强制转换为整数:

a =  (int) System.in.read();
System.in.read返回一个整数,但结果将是字符1的ASCII码,即49

我建议使用扫描仪:

Scanner s = new Scanner(System.in);
a = s.nextInt();

输入ASCII字符:1。当转换为int时,它的值为49。同样,输入2时得到50,输入空格等时得到32。提示:“1”的ASCII码是49。输入ASCII字符:1。当转换为int时,它的值为49。同样,当你输入2时会得到50,当输入空格时会得到32,等等。提示:“1”的ASCII码是49。建议使用扫描仪+1,但据我所知,System.in.read返回的是一个整数,而不是一个字节!是的,你确实是对的。应该仔细看看。我会更正我的答案。建议使用扫描仪是好的+1,但据我所知,System.in.read返回的是一个整数,而不是一个字节!是的,你确实是对的。应该仔细看看。我会更正我的答案。