程序控制 《大學我玩夠了你呢?30天輕鬆搞定 Java》

第三章 Java程序控制

一、循環控制

在這裏插入圖片描述
代碼展示

public static void main(String[] args) {
        //while循環,先判斷,再執行
        int i = 5;
        System.out.print("倒計時開始");
        while (i > 0) {
            System.out.print(i+" ");
            i--;
        }
        System.out.print("發射!");
        System.out.println();

        //do-while循環,先執行再判斷
        int j=5;
        System.out.print("倒計時開始");
        do {
                System.out.print(j+" ");
                j--;
        } while (j>0);
        System.out.print("發射!");
        System.out.println();

        //for循環
        String s = new String("5201314");
        char arr[] = s.toCharArray();
        for (i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        //for each 循環
        for (char a:arr){
            System.out.print(a+" ");
        }
        System.out.println();
        //for 嵌套循環
        for (int m=1;m<=9;m++){
            for (int n=1;n<=m;n++) {
                System.out.print(n+"*"+m+"="+m*n+" ");
            }
            System.out.println();
        }
    }

二、判斷控制

在這裏插入圖片描述

 public static void main(String[] args) {
        //if語句
        System.out.println("請輸入您的性別:");
        Scanner scanner = new Scanner(System.in);
        String s=scanner.next();
        if(s.equals("男")){
            System.out.println("跪求歐巴點個贊丫");
        } else if (s.equals("女")) {
            System.out.println("跪求仙女姐姐點個贊丫");
        } else{
            System.out.println("您輸入的性別有誤");
        }
        //swicth 語句  表達式的值必須是整型或者字符型的變量
        for (int i=1;i<=6;i++){
            switch (i){
                case 1:
                    System.out.println("恭喜少俠通過第1關!");
                    break;
                case 2:
                    System.out.println("恭喜少俠通過第2關!");
                    break;
                case 3:
                    System.out.println("恭喜少俠通過第3關!");
                    break;
                case 4:
                    System.out.println("恭喜少俠通過第4關!");
                    break;
                case 5:
                    System.out.println("恭喜少俠通過最後一個關卡!您獲得了本場遊戲的勝利,恭喜您!");
                    break;
                default:
                    System.out.println("尊敬的少俠,由於數據異常,本場遊戲被迫中止,非常抱歉!");
            }
        }
    }

代碼展示

三、 跳轉控制

在這裏插入圖片描述
代碼展示

public static void main(String[] args) {
//break語句
        // 猜數字
        System.out.println("少俠,小的已經爲您成功生成了一個數字,範圍在[0,100),猜一猜吧!");
        int res = (int) (100*Math.random());
        System.out.println("答案是: "+res);
        Scanner scanner = new Scanner(System.in);
        int num,count = 0;
        while(true){
            num = scanner.nextInt();
            count++;
            if (num==res) {
                System.out.println("恭喜少俠,您猜對了!您一共猜了 "+count+" 次。");
                break;//強制中斷當前的循環
            } else if(num>res) {
                System.out.println("少俠,你猜的數‘ "+num+" ’ 太大了。用點子智慧哦!您已經猜了 "+count+" 次。");
            }else {
                System.out.println("少俠,你猜的數‘ "+num+" ’太小了。用點子智慧哦!您已經猜了 "+count+" 次。");
            }
        }
//continue語句
        //計算1~10內的所有奇數和
        int sum=0;
        for(int i=1;i<=10;i++){
            if(i%2==0){
                continue;//不在執行continue之後的語句,再重新判斷循環條件,繼續循環
            }
            System.out.print(i+" ");
            sum = sum + i;
        }
        System.out.println("sum= "+sum);
//return語句
        for (int i = 1; i <= 5;i++) {
            System.out.print("i="+i+" ");
            if (i==5){
                return;//return語句。將程序控制跳轉到方法的調用者
            }
        }
        System.out.println("因爲有return,所以這句話不會輸出!");
    }

總結

學習從點滴開始,學到的東西可能會忘記,記得點贊收藏哦
在這裏插入圖片描述

System.out.println("我選擇滑稽取寵");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章