18

System.in.read()在java中有什么用?
请解释一下。

4

10 回答 10

41

迟到两年半总比没有好,对吧?

int System.in.read()从输入流中读取数据的下一个字节。但我相信你已经知道了,因为查找起来很简单。所以,你可能要问的是:

  • int当文档说它读取 a 时,为什么要声明它返回a byte

  • 为什么它似乎返回垃圾?(我输入'9',但它返回57。)

它返回一个,int因为除了一个字节的所有可能值之外,它还需要能够返回一个额外的值来指示流结束。因此,它必须返回一个可以表达比bytecan 更多的值的类型。

注意:他们本可以把它做成 a short,但他们选择了int,可能是作为对 C 具有历史意义的帽子的一角,它的getc()函数也返回 a int,但更重要short的是因为使用起来有点麻烦,(该语言提供无法指定short文字,因此您必须指定int文字并将其强制转换为short,) 加上在某些架构int上比short.

它似乎返回垃圾,因为当您将字符视为整数时,您正在查看的是该字符的 ASCII (*)值。因此,“9”显示为 57。但是如果将其转换为角色,则会得到“9”,所以一切都很好。

可以这样想:如果您输入字符 '9',期望System.in.read()返回数字 9 是荒谬的,因为如果您输入了 an ,您希望它返回什么数字'a'?显然,字符必须映射到数字。ASCII (*)是一种将字符映射到数字的系统。在这个系统中,字符“9”映射到数字 57,而不是数字 9。

(*) 不一定是 ASCII;可能是其他编码,例如 UTF-16;但在绝大多数编码中,当然在所有流行的编码中,前 127 个值与 ASCII 相同。这包括所有英文字母数字字符和流行符号。

于 2015-12-06T17:49:40.350 回答
29

可能这个例子会对你有所帮助。

import java.io.IOException;

public class MainClass {

    public static void main(String[] args) {
        int inChar;
        System.out.println("Enter a Character:");
        try {
            inChar = System.in.read();
            System.out.print("You entered ");
            System.out.println(inChar);
        }
        catch (IOException e){
            System.out.println("Error reading from user");
        }
    }
}
于 2013-03-16T07:26:40.873 回答
5

Systemjava.lang包中的最后一个类

来自 api 源代码的代码示例

public final class System {

   /**
     * The "standard" input stream. This stream is already
     * open and ready to supply input data. Typically this stream
     * corresponds to keyboard input or another input source specified by
     * the host environment or user.
     */
    public final static InputStream in = nullInputStream();

}

read()是抽象类的抽象方法InputStream

 /**
     * Reads the next byte of data from the input stream. The value byte is
     * returned as an <code>int</code> in the range <code>0</code> to
     * <code>255</code>. If no byte is available because the end of the stream
     * has been reached, the value <code>-1</code> is returned. This method
     * blocks until input data is available, the end of the stream is detected,
     * or an exception is thrown.
     *
     * <p> A subclass must provide an implementation of this method.
     *
     * @return     the next byte of data, or <code>-1</code> if the end of the
     *             stream is reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public abstract int read() throws IOException;

简而言之,来自 api:

从输入流中读取一些字节并将它们存储到缓冲区数组 b. 实际读取的字节数以整数形式返回。在输入数据可用、检测到文件结尾或引发异常之前,此方法会一直阻塞。

来自InputStream.html#read()

于 2013-03-16T07:37:27.507 回答
4

只是为了补充接受的答案,您也可以System.out.read()这样使用:

class Example {
    public static void main(String args[])
        throws java.io.IOException { // This works! No need to use try{// ...}catch(IOException ex){// ...}         

        System.out.println("Type a letter: ");
        char letter = (char) System.in.read();
        System.out.println("You typed the letter " + letter);
    }
}
于 2018-02-04T20:56:11.407 回答
4
import java.io.IOException;

class ExamTest{

    public static void main(String args[]) throws IOException{
        int sn=System.in.read();
        System.out.println(sn);
    }
}

如果要获取字符输入,则必须像这样进行转换:char sn=(char) System.in.read()

值 byte 以 int 形式返回,范围为 0 到 255。但是,与其他语言的方法不同,它System.in.read()一次只读取一个字节。

于 2018-08-04T06:30:28.263 回答
2

System.in.read()标准输入读取。

标准输入可用于在控制台环境中从用户那里获取输入,但由于此类用户界面没有编辑功能,因此标准输入的交互式使用仅限于教授编程的课程。

标准输入的大多数生产用途是在设计用于在 Unix 命令行管道中工作的程序中。在这样的程序中,程序正在处理的有效负载来自标准输入,程序的结果被写入标准输出。在这种情况下,标准输入永远不会由用户直接编写,它是另一个程序的重定向输出或文件的内容。

一个典型的管道如下所示:

# list files and directories ordered by increasing size
du -s * | sort -n

sort从标准输入读取它的数据,这实际上是du命令的输出。排序后的数据被写入 的标准输出sort,默认情况下最终在控制台上,并且可以轻松地重定向到文件或另一个命令。

因此,标准输入在 Java 中相对很少使用。

于 2013-03-16T07:26:07.730 回答
1

这个例子应该有帮助吗?除了评论,当然>:)

警告:在本段/帖子中,人是一个被过度使用的常用词

总的来说我推荐使用这个Scanner类,因为你可以输入大句子,我不完全确定System.in.read有这些方面。如果可能,请纠正我。

public class InputApp {

// Don't worry, passing in args in the main method as one of the arguments isn't required MAN

    public static void main(String[] argumentalManWithAManDisorder){
        char inputManAger;

        System.out.println("Input Some Crap Man: ");
        try{
            // If you forget to cast char you will FAIL YOUR TASK MAN
            inputManAger = (char) System.in.read();
            System.out.print("You entererd " + inputManAger + " MAN");
        }
        catch(Exception e){
            System.out.println("ELEMENTARY SCHOOL MAN");
        }
    }
}
于 2015-10-16T13:05:13.837 回答
0

System.in.read() 是 System.in 类的读取输入方法,它是“标准输入文件”或传统操作系统中的 0。

于 2014-11-18T10:48:39.433 回答
0

它允许您从标准输入(主要是控制台)读取。这个 SO question可能会对您有所帮助。

于 2013-03-16T07:27:41.867 回答
0

system.in.read() 方法读取一个字节并以整数形式返回,但如果您输入 1 到 9 之间的 no ,它将返回 48+ 个值,因为在 ascii 代码表中,ascii 值 1-9 是 48-57 。希望,它会有所帮助。

于 2019-06-30T08:43:10.967 回答