Java System.in.read()方法

Java System.in.read()方法,java,Java,下面是代码,我已经编写了从用户那里获得两个输入。但当我运行程序时,它只接受一个输入,自己生成另一个,然后计算错误的值。 请帮忙。谢谢 import java.io.IOException; import java.util.*; class ThrowsExcpt { int divide(int x, int y) throws ArithmeticException, IOException { return x / y; } } class Throws

下面是代码,我已经编写了从用户那里获得两个输入。但当我运行程序时,它只接受一个输入,自己生成另一个,然后计算错误的值。 请帮忙。谢谢

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);
    }
}

尝试使用扫描仪对象读取用户的输入:

Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();

System.in
是一个
InputStream
-
read()
只读取一个字节。您的直接输入超过一个字节,因此这两个值在第一个输入中直接读取

改用扫描仪(输入System.in):

更多示例: Read()方法

从输入流读取数据的下一个字节。值字节作为0到255范围内的整数返回。如果由于到达流的结尾而没有字节可用,则返回值-1。此方法会一直阻塞,直到输入数据可用、检测到流结束或引发异常为止

因此,请使用
scanner
进行此操作

根据你的问题,为什么不要求第二次输入?原因是read()方法wait for input source一旦获得输入,就会从该输入源逐字节获取。例如:

        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
输出,即a、b和c的字节值

说明:输入
abc
字节数组

+--------------------+
| 97 | 98 | 99 | other byte value for **Enter**
+--------------------+ 

第一个System.in.read()将获得数组的第一个索引,第二个将获得第二个索引,依此类推。

嘿,兄弟,这应该可以满足您的需要,我想如果它不让我知道的话

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();
自动抛出异常

  • 您分配并定义了一个内存单元来保存一个int,然后分配了一个字节存储在那里。重铸输入或使用不同的输入方法
为什么会发生不想要的任务?

按下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

如果我已经写了两次read()方法,请检查此项。为什么它没有得到两个输入。因为一个读取()读取一个单字节/字符-因此,如果输入12,它将被读取为(字符)1和2-因此它完成了两个读取()调用(“1”是49,“2”是50)-使用Scanner和nextInt,您已经得到了正确的值,而不是字符代码,但我只作为输入4输入。它本身需要其他输入。并且进行计算是的,返回值作为第二个输入读取return应该是13,4的代码是52-所以你计算52/13扫描器对代码的处理非常慢,但是我只需要两个用户在read()方法的帮助下输入。我可以在read()方法的帮助下使用console获取两个用户输入。请建议。这里不是指ASCII表,而是更相关的参考。
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');