1. 程式人生 > >Scanner的概述和方法介紹

Scanner的概述和方法介紹

數據 tint sys 標記 strong new can 取字符 col

Scanner的概述和方法介紹

一:Scanner的構造方法原理

   1.Scanner(InputStream source)  

      構造一個新的 Scanner,它生成的值是從指定的輸入流掃描的。

   2. System類下有一個靜態的字段:public static final InputStream in

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

二:一般方法

    hasNextXxx():判斷是否還有下一個輸入項

    NextXxx():獲取下一個輸入項

public
static void main(String[] args) { Scanner sc = new Scanner(System.in); if (sc.hasNextInt()) { int i = sc.nextInt(); System.out.println(); } else { System.out.println("輸入錯誤!"); } }

三:Scanner獲取數據出現的小問題及解決方案

  1:三個常用的方法:     

     public int nextInt():獲取一個int類型的值


    public String nextLine():獲取一個String類型的值
    public String next():查找並返回來自此掃描器的下一個完整標記。
    *當鍵盤錄入整數和字符串時,讀取字符串的方法用next();因為nextLine()一遇到換行符就結束了.

    

Scanner的概述和方法介紹