System.in.read()方法
以下是代码,我写的是从用户那里得到两个输入。 但是当我运行程序时,它只需要一个输入并自己生成另一个并计算错误的值。 请帮忙。 谢谢
import java.io.IOException; import java.util.*; class ThrowsExcpt { int divide(int x, int y) throws ArithmeticException, IOException { return x / y; } } class ThrowsTemps { public static void main(String s[]) throws IOException { int x = System.in.read(); int y = System.in.read(); ThrowsExcpt th = new ThrowsExcpt(); int r = th.divide(x, y); System.out.println(r); } }
System.in
是一个InputStream
– read()
读取一个字节。 您的直接输入不止一个字节,因此在第一个输入中直接读取这两个值。
改为使用扫描仪(使用System.in作为输入):
Scanner sc = new Scanner(System.in); int i = sc.nextInt();
更多示例: http : //docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
Read()方法。
从输入流中读取下一个数据字节。 值字节作为int返回,范围为0到255.如果没有字节可用,因为已到达流的末尾,则返回值-1。 此方法将阻塞,直到输入数据可用,检测到流的末尾或抛出exception。
来自http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29 。
所以使用scanner
。
并根据你的问题为什么不要求第二次输入? 原因是read()方法等待输入源一旦获得输入,它就从该输入源逐字节获取。 例如:
inChar = System.in.read(); i = System.in.read(); i1 = System.in.read(); System.out.print("You entered "); System.out.println(inChar); System.out.println(i); System.out.println(i1);
你输入abc
作为输入然后你会得到
Enter a Character: abc You entered 97 98 99
as out是a,b和c的字节值。
说明:输入abc
字节数组
+--------------------+ | 97 | 98 | 99 | other byte value for **Enter** +--------------------+
首先,System.in.read()将获得第一个数组索引,第二个将获得第二个索引,依此类推。
为什么要进行不需要的分配?
按下ENTER后,键盘输入字符存储在输入缓冲区中。 ENTER包括输入字符中的换行符’\ n’。 换行符’\ n’的十进制/整数值为10。
System.in.read()只读取输入字符中的第一个字符,该字符被转换为整数并返回它。 并且该字符将从输入缓冲区中排除。
其余字符仍然在输入缓冲区中挂起,直到System.in.read()以与上述相同的方式读取它。
int inputchar1, inputchar2, inputchar3; System.out.print("input: ");// input: 43 // The line feed '\n' is generated when ENTER is pressed. // '4', '3' and '\n' are in the input buffer. inputchar1 = System.in.read(); // '4' is read and returns 52 (corresponding integer value) // '3' and '\n' is in the input buffer. inputchar2 = System.in.read(); // '3' is read and returns 51 // '\n' is pending in the input buffer. inputchar3 = System.in.read(); // '\n' is read and returns 10 // no more input character in the input buffer. System.out.println("inp1 =" + inputchar1); System.out.println("inp1 =" + inputchar2); System.out.println("inp1 =" + inputchar3); /* * Refer to ASCII table for char to decimal conversion. * Although java Char is 16-bit unicode ASCII is still applicable. */
如何避免不必要的任务?
继续读取输入缓冲区的内容,直到它耗尽为止。
int x, y, avoid; System.out.println("Enter x: "); x = System.in.read(); // Reading all the characters after the first character so that // no character stays pending in the input buffer. do { avoid = System.in.read(); } while(avoid != '\n'); System.out.println("Enter y: "); // New input buffer is created. y = System.in.read(); do { avoid = System.in.read(); } while(avoid != '\n');
参考:“Java:初学者指南6e” – Herbert Schildt
尝试使用Scanner对象来读取用户的输入:
Scanner scanner = new Scanner(System.in); int x = scanner.nextInt();
如果您使用的是控制台,请尝试以下操作:
int x = Integer.parseInt(System.console().readLine()); int y = Integer.parseInt(System.console().readLine());
嘿兄弟如果它不让我知道,这应该适用于我需要的东西
import java.io.IOException; import java.util.Scanner; public class noob{ class ThrowsExcpt { int divide(int x, int y) throws ArithmeticException, IOException { return x / y; } } class ThrowsTemps { public void main(String s[]) throws IOException { Scanner sc = new Scanner(System.in); System.out.println("Enter number 1"); int x = sc.nextInt(); System.out.println("Enter number 2"); int y = sc.nextInt();
简单的说 –
int x = System.in.read();
自动抛出exception。
- 你分配并定义了一个存储单元来保存一个int,然后分配一个字节存储在那里。 重新输入输入或使用差异。 输入法。