亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么在實例化 Scanner 類的新對象時將 (System.in) 作為參數傳遞?

為什么在實例化 Scanner 類的新對象時將 (System.in) 作為參數傳遞?

慕村225694 2023-07-19 16:56:11
如果在掃描儀類的實例化中沒有 System.in,我無法從鍵盤分配任何值。究竟發生了什么。任何人都可以詳細說明嗎?
查看完整描述

3 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

的值System.in是 的實例InputStream,是“標準輸入”。正如文檔所說:

“標準”輸入流。該流已打開并準備好提供輸入數據。通常,該流對應于鍵盤輸入或主機環境或用戶指定的另一個輸入源。

當您使用時,new Scanner(System.in)您正在使用Scanner#<init>(InputStream)構造函數,其文檔說:

構造一個 newScanner來生成從指定輸入流掃描的值。使用底層平臺的默認字符集將流中的字節轉換為字符。

如您所見,傳遞System.in只是將其配置Scanner為從標準輸入讀取。您當然可以使用InputStream從其他地方(例如網絡、文件等)讀取的不同內容。如果您查看其他構造函數,Scanner您會發現您可以配置許多不同類型的源(例如文件、String等)。


查看完整回答
反對 回復 2023-07-19
?
Qyouu

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;

希望這是不言自明的!


查看完整回答
反對 回復 2023-07-19
?
慕碼人8056858

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


查看完整回答
反對 回復 2023-07-19
  • 3 回答
  • 0 關注
  • 244 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號