java筆記7之錄入

來源:互聯網
上載者:User

標籤:code   pre   main   dem   比較   資料   個數   span   out   


1  為了讓程式的資料更符合開發的資料,我們就加入了鍵盤錄入。
    讓程式更靈活一下。
    
    那麼,我們如何?鍵盤資料的錄入呢?
        A:導包
            格式:
                import java.util.Scanner;
            位置:
                在class上面。
        B:建立鍵盤錄入對象
            格式:
                Scanner sc = new Scanner(System.in);
        C:通過對象擷取資料    
            格式:
                int x = sc.nextInt();

import java.util.Scanner;class ScannerDemo {    public static void main(String[] args) {        //建立鍵盤錄入資料對象        Scanner sc = new Scanner(System.in);                System.out.println("請你輸入一個資料:");        int x = sc.nextInt();                System.out.println("你輸入的資料是:"+x);    }}
 1 /* 2     練習: 3         鍵盤錄入三個資料,擷取這三個資料中的最大值 4         鍵盤錄入兩個資料,比較這兩個資料是否相等 5 */ 6 import java.util.Scanner; 7  8 class ScannerTest3 { 9     public static void main(String[] args) {10         //鍵盤錄入三個資料,擷取這三個資料中的最大值11     12         //建立鍵盤錄入對象13         Scanner sc = new Scanner(System.in);14         15         System.out.println("請輸入第一個資料:");16         int a = sc.nextInt();17         18         System.out.println("請輸入第二個資料:");19         int b = sc.nextInt();20         21         System.out.println("請輸入第三個資料:");22         int c = sc.nextInt();23         24         //擷取這三個資料中的最大值25         int temp = ((a > b)? a: b);26         int max = (temp > c? temp : c);27         System.out.println("max:"+max);28         System.out.println("------------------");29         30         //鍵盤錄入兩個資料31         System.out.println("請輸入第一個資料:");32         int x = sc.nextInt();33         34         System.out.println("請輸入第二個資料:");35         int y = sc.nextInt();36         37         //比較這兩個資料是否相等38         boolean flag = (x == y);39         System.out.println("flag:"+flag);40     }41 }

2

java筆記7之錄入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.