簡體   English   中英

掃描儀實際上並未接受用戶輸入

[英]Scanner not actually taking in user input

我嘗試過掃描儀的不同用途(我希望它在文件中讀取,但我也只嘗試過字符串),它只是跳過代碼,就好像它不存在一樣。 不顯示錯誤消息。

   public static void main(String[] args)
      throws FileNotFoundException {
      
      Scanner test = new Scanner(System.in);
      String testLine = test.next();

      Scanner input = new Scanner(new File("data.txt"));

      while(input.hasNextLine){
         String name = input.nextLine();
         String letters = input.nextLine();
         System.out.println(name + ": " + letters);
      }
   }

您的代碼無法編譯,因為hasNextLine()是 function,而不是 class 成員。

您實際上是在test.next();處從System.in讀取數據; - 您必須輸入一些文本,然后您的代碼將繼續運行。 它只是在等待用戶輸入 - 因此不會引發異常。

在這行代碼:

String testLine = test.next();

您的程序正在等待您的輸入。 在您提供輸入之前,它無法進入下一行。

編輯:
從下面查理的評論中得到啟示,這里是來自文檔的關於System.in的引用。

“標准”輸入 stream。 這個 stream 已經打開並准備好提供輸入數據。 通常,此 stream 對應於鍵盤輸入或主機環境或用戶指定的其他輸入源。

更多在這里。 .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM