Android(java)學習筆記97:Scanner類使用

來源:互聯網
上載者:User

標籤:

 1 package cn.itcast_01; 2  3 /* 4  * Scanner:用於接收鍵盤錄入資料。 5  *  6  * 前面的時候: 7  *         A:導包 8  *         B:建立對象 9  *         C:調用方法10  * 11  * System類下有一個靜態欄位:12  *         public static final InputStream in; 標準的輸入資料流,對應著鍵盤錄入。13  * 14  *         InputStream is = System.in;15  * 16  * class Demo {17  *         public static final int x = 10;18  *         public static final Student s = new Student();19  * }20  * int y = Demo.x;21  * Student s = Demo.s;22  * 23  * 24  * 構造方法:25  *         Scanner(InputStream source)26  */27 import java.util.Scanner;28 29 public class ScannerDemo {30     public static void main(String[] args) {31         // 建立對象32         Scanner sc = new Scanner(System.in);33 34         int x = sc.nextInt();35         36         System.out.println("x:" + x);37     }38 }

 

 1 package cn.itcast_02; 2  3 import java.util.Scanner; 4  5 /* 6  * 基本格式: 7  *         public boolean hasNextXxx():判斷是否是某種類型的元素 8  *         public Xxx nextXxx():擷取該元素 9  * 10  * 舉例:用int類型的方法舉例11  *         public boolean hasNextInt()12  *         public int nextInt()13  * 14  * 注意:15  *         InputMismatchException:輸入的和你想要的不匹配16  */17 public class ScannerDemo {18     public static void main(String[] args) {19         // 建立對象20         Scanner sc = new Scanner(System.in);21 22         // 擷取資料23         if (sc.hasNextInt()) {24             int x = sc.nextInt();25             System.out.println("x:" + x);26         } else {27             System.out.println("你輸入的資料有誤");28         }29     }30 }

 

Android(java)學習筆記97:Scanner類使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.