3 回答

TA貢獻1809條經驗 獲得超8個贊
的值System.in
是 的實例InputStream
,是“標準輸入”。正如文檔所說:
“標準”輸入流。該流已打開并準備好提供輸入數據。通常,該流對應于鍵盤輸入或主機環境或用戶指定的另一個輸入源。
當您使用時,new Scanner(System.in)
您正在使用Scanner#<init>(InputStream)
構造函數,其文檔說:
構造一個 new
Scanner
來生成從指定輸入流掃描的值。使用底層平臺的默認字符集將流中的字節轉換為字符。
如您所見,傳遞System.in
只是將其配置Scanner
為從標準輸入讀取。您當然可以使用InputStream
從其他地方(例如網絡、文件等)讀取的不同內容。如果您查看其他構造函數,Scanner
您會發現您可以配置許多不同類型的源(例如文件、String
等)。

TA貢獻1786條經驗 獲得超11個贊
因為你需要指定從哪里讀取。
以下陳述完全有效并來自各種來源
Scanner?sc?=?new?Scanner(new?File("myNumbers"));
String?input?=?"1?fish?2?fish?red?fish?blue?fish";Scanner?s?=?new?Scanner(input);
Scanner?sc?=?new?Scanner(System.in);
所以,現在您必須清楚,在實例化 Scanner 對象時System.in
?不必總是作為參數。
繼續,解釋你為什么通過System.in
來自文檔
公共掃描儀(InputStream源)
構造一個新的 Scanner,它生成從指定輸入流掃描的值。使用底層平臺的默認字符集將流中的字節轉換為字符。
從java.lang.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 = null;
希望這是不言自明的!

TA貢獻1803條經驗 獲得超6個贊
來自 Javadoc:類掃描器
| Constructor? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| Description? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
|---------------------------------------------------------|----------------------------------------------------------------------------------------|
| Scanner(File source)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Constructs a new Scanner that produces values scanned from the specified file.? ? ? ? ?|
| Scanner(File source, String charsetName)? ? ? ? ? ? ? ? | Constructs a new Scanner that produces values scanned from the specified file.? ? ? ? ?|
| Scanner(File source, Charset charset)? ? ? ? ? ? ? ? ? ?| Constructs a new Scanner that produces values scanned from the specified file.? ? ? ? ?|
| Scanner(InputStream source)? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| Constructs a new Scanner that produces values scanned from the specified input stream. |
| Scanner(InputStream source, String charsetName)? ? ? ? ?| Constructs a new Scanner that produces values scanned from the specified input stream. |
| Scanner(InputStream source, Charset charset)? ? ? ? ? ? | Constructs a new Scanner that produces values scanned from the specified input stream. |
| Scanner(Readable source)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Constructs a new Scanner that produces values scanned from the specified source.? ? ? ?|
| Scanner(String source)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Constructs a new Scanner that produces values scanned from the specified string.? ? ? ?|
| Scanner(ReadableByteChannel source)? ? ? ? ? ? ? ? ? ? ?| Constructs a new Scanner that produces values scanned from the specified channel.? ? ? |
| Scanner(ReadableByteChannel source, String charsetName) | Constructs a new Scanner that produces values scanned from the specified channel.? ? ? |
| Scanner(Path source)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Constructs a new Scanner that produces values scanned from the specified file.? ? ? ? ?|
| Scanner(Path source, String charsetName)? ? ? ? ? ? ? ? | Constructs a new Scanner that produces values scanned from the specified file.? ? ? ? ?|
| Scanner(Path source, Charset charset)? ? ? ? ? ? ? ? ? ?| Constructs a new Scanner that produces values scanned from the specified file.? ? ? ? ?|
要指定您想要哪種類型的掃描儀,您必須傳遞一個參數。System.in 告訴 Scanner 類,從InputStream source
添加回答
舉報