JAVA基礎 - 鍵盤錄入與分支、循環結構案例

一、鍵盤錄入

    public static void main(String[] args) {
        //案例:鍵盤錄入技術
        //得到一個鍵盤掃描器對象
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入您的年齡:");
        //調用sc對象的功能等待接收用戶輸入的數據
        //這個代碼會等待用戶輸入數據,直到用戶輸入完數據並按了回車鍵就會把數據拿到
        int age = sc.nextInt();
        System.out.println("您的年齡是:" + age);
        System.out.println("請輸入您的名字");
        String name = sc.next();
        System.out.println("歡迎" + name);
        }

二、分支結構

1、if分支:根據判斷的結果(真或假)決定執行某個分支的代碼

public static void main(String[] args) {
        //分支結構,格式1:if(條件表達式){代碼...}
        // 需求:心跳(60-100)之間是正常的,否則系統提示進一步檢查
        int hearBeat = 30;
        if (hearBeat < 60 || hearBeat > 100) {
            System.out.println("系統檢測到您的心跳有異常,請進一步檢查,當前心跳係數爲:" + hearBeat);
        }
        System.out.println("檢查結束");
//分支結構,格式2:if(條件表達式){代碼...} else{代碼... //需求:發紅包,有錢就發,沒錢就不發 double money = 1000.11; if (money>=1314){ System.out.println("您的紅包已發送"); } else { System.out.println("沒錢就別發了"); }
//分支結構,格式3:if(條件表達式){代碼...} elseif(條件表達式){代碼...} else{代碼...} //需求:績效系統 0-60 C 60-80 B 80-90 A 90-100 A+ int score = 160; if(score>=90&&score<=100){ System.out.println("您的績效爲:A+"); } else if(score>=80&&score<90){ System.out.println("您的績效爲A"); } else if(score>=60&&score<80){ System.out.println("您的績效爲B"); } else if(score>=0&&score<60){ System.out.println("您的績效爲C"); } else { System.out.println("您的輸入的成績不在0-100範圍內"); } }

2、switch

  if其實在功能上遠遠強大於switch,if適合做區間匹配,switch適合做 值匹配的分支選擇、代碼優雅

  switch分支注意事項:

  • 表達式類型只能是byte,short,int,char,jdk5開始支持枚舉,jdk7開始支持string,不支持double,float,long
  • case給出的值不允許重複,且只能是字面量,不能是變量
  • 不要忘了寫break,否則會出現穿透現象
public static void main(String[] args) {
        //switch(表達式){case值:執行代碼}
        String week = "週二";
        switch (week){
            case "週一":
                System.out.println("一週的第一天");
                break;
            case "週二":
                System.out.println("今天週二啦");
                break;
            default:
                System.out.println("輸入的不是週數據");
        }
}

3、switch的穿透性

  如果代碼執行到沒有寫break的case塊,執行完後將直接進入下一個case塊執行代碼(而且不會進行任何匹配),直到遇到breask才跳出分支,這就是switch的穿透性

三、循環結構

1、for循環

public static void main(String[] args) {
        //for循環,for(初始化語句;循環條件;迭代語句){循環體語句}
        //需求1:計算1-5的和
        int sum =0;
        for(int i=1;i<=5;i++){
            sum += i; //把數據加給自己,用+=,即sum=sum+i
        }
        System.out.println("1-5的和是"+sum);

        //需求2:求1-10的奇數之和,並把求和結果在控制檯輸出
        //分析:1、定義for循環使其能夠依次訪問到1-10的數;
        // 2、在for循環中,通過if篩選出奇數;
        // 3、在循環外定義一個整數變量sum來累加這些數據
        int count =0;
        for(int i=1;i<10;i++){
            if(i%2==1){
                count +=i;
            }
        }
        System.out.println("1-10的奇數之和是"+count);


        //需求3:找出水仙花數並輸出(1、水仙花數是一個三位數;2、水仙花數的個位、十位、百位的數字立方和等於原數)
        //分析:1、定義一個for循環從100一直到999
        //2、每次訪問到數據後,提取該數據的:個位、十位、百位數字
        //3、使用if判斷:個位、十位、百位的數字立方和是否等於原數,等於則輸出該數據
        count = 0;// 統計水仙花的個數
        for(int i=100;i<=999;i++){
            int ge = i % 10;//取餘,比如157%10=15.7,取餘即7
            int shi = i / 10 % 10; //比如157/10=15,15%10=1.5,最後取餘爲5
            int bai = i / 100;//比如157/100=1 (保留整數)
            if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
                System.out.print(i+"\t");//"\t"是空格特殊字符,注意如果要讓輸出內容在同一行,要把print的ln去掉
                //如果想知道水仙花數的個數,可以在循環外定義一個變量count用於記錄水仙花數,每輸出水仙花數時,讓count++
                count ++;
            }
        }
        System.out.println();//換行
        System.out.println("水仙花的個數爲"+count);
}

2、while循環

  使用規範:知道循環幾次,使用for;不知道循環幾次建議使用while

public static void main(String[] args) {
//需求1:珠穆朗瑪高度是8848860,紙張厚度是0.1,摺疊紙張直到不低於珠穆朗瑪位置,求摺疊幾次(不知道循環多少次的時候用while)
        //3、定義一個變量用於記錄紙張摺疊的次數
        int countpeak = 0;
        //1、定義變量記錄山峯的高度、紙張的百度
        double peakHeight = 8848860;//山峯高度
        double paperThickness = 0.1;//紙張厚度
        //2、定義一個while循環控制紙張進行摺疊
        while (paperThickness<peakHeight){
            paperThickness *= 2;//每摺疊一次,紙張厚度爲原來的2倍
            countpeak ++;
        }
        System.out.println("摺疊次數爲"+countpeak);
        System.out.println("紙張最終的厚度"+paperThickness);
}

 

3、do-while循環

  先執行再判斷循環條件

int i = 0;
do{
  System.out.println("hello");  
  i++;  
}while(i<3);

4、死循環、循環嵌套、break、continue

   死循環:一直循環的執行下去,如果沒有干預不會停止下來

  嵌套循環:外部循環每循環一次,內部循環全部執行完一次。

  跳轉控制語句介紹:

  • break:跳出並結束當前所在循環的執行(注意:只能用於結束所在循環,或者結束所在switch分支的執行)
  • continue:用於跳出當前循環的當前執行,進入下一次循環。(注意:只能在循環中進行使用)
public static void main(String[] args) {
//死循環需求:系統密碼是520,請用戶不斷輸入密碼驗證,驗證不對輸出密碼錯誤,驗證成功輸出歡迎進入系統,並停止程序
        int okPassword = 520;//定義正確的密碼
        Scanner scpa = new Scanner(System.in);
        while (true){   //定義死循環讓用戶不斷輸入密碼
            System.out.println("請您輸入正確的密碼:");
            int password = scpa.nextInt();
            if(password==okPassword){
                System.out.println("登錄成功了");
                break;
            }else {
                System.out.println("密碼錯誤");
            }
        }

        //嵌套循環:5天,每天說3遍我要學習
        for(int i=0;i<=5;i++){
            for(int j=0;j<=3;j++){
                System.out.println("我要學習");
            }
        }
}

四、案例技術

1、Random的使用

public static void main(String[] args) {
//隨機數類Random:生成0-10之間的隨機數
        Random r = new Random();
        int data = r.nextInt(10);//0-9 不包含10(包前不包後)
        int number =r.nextInt(10)+1;//1-0,生成1-10之間的隨機數
        int number1 = r.nextInt(27)+65;//生成65-91之間的隨機數,減加法: 65-91轉換成 (0-26)+65
        System.out.println("生成的隨機數爲:"+data);
        System.out.println("生成的隨機數爲:"+number);
}

 

2、猜數字遊戲

public static void main(String[] args) {
//猜數字遊戲
        Random num = new Random();
        int numdata = r.nextInt(100)+1;//隨機生成一個數字1-100之間 (0-99)+1
        Scanner numsc = new Scanner(System.in);
        while (true){  //使用一個死循環讓用戶不斷的去猜測,並給出提示
            System.out.println("請您輸入猜測的數據(1-100):");
            int guessNumber = numsc.nextInt();
            if(guessNumber<numdata){
                System.out.println("您猜測的過小了");
            }else if(guessNumber>numdata){
                System.out.println("您猜測的過大了");
            }else {
                System.out.println("恭喜您猜中了");
                break;//直接跳出並結束當前死循環
            }
        }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章