編碼練習——Java-2-流程式控制制__Ajax

來源:互聯網
上載者:User

歡迎訪問 部落格新址 條件 條件陳述式 - if

public class Test {    public static void main(String[] args) {        int a = 100;        if(a==100) {            System.out.println("a的值是 100");        }    }}
public class Test {    public static void main(String[] args) {        int x = 45;        int y = 12;        if (x>y) {            System.out.println("變數x 大於 變數y");        }        if (x<y) {            System.out.println("變數x 小於 變數y");        }    }}
public class Test {    public static void main(String[] args) {        int math = 95;        if (math>60) {            System.out.println("數學及格了");        } else {            System.out.println("數學沒有及格");        }    }}
public class Test {    public static void main(String[] args) {        int x = 20;        if(x>30) {            System.out.println("a的值大於30");        } else if (x>10) {            System.out.println("a的值大於10,但小於30");        } else if (x>0) {            System.out.println("a的值大於0,但小於10");        } else {            System.out.println("a的值小於0");        }    }}
條件陳述式 - switch
public class Test {    public static void main(String[] args) {        System.out.println("今天是星期幾:");        int week = 2;        switch(week) {        case 1:            System.out.println("Monday");            break;        case 2:            System.out.println("Tuesday");            break;        case 3:            System.out.println("Wednesday");            break;        default:            System.out.println("Sorry, I don't know");        }    }}
執行個體 - 驗證登入資訊的合法性
import java.util.Scanner;public class Test {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        System.out.println("請輸入登入使用者名稱:");        String username = scan.nextLine();        System.out.println("請輸入登入密碼:");        String password = scan.nextLine();        if (!username.equals("mr")) {            System.out.println("使用者名稱非法");        } else if (!password.equals("mrsoft")) {            System.out.println("登入密碼錯誤");        } else {            System.out.println("恭喜您, 登入資訊通過驗證");        }    }}
執行個體 - 為新員工分配部門
import java.util.Scanner;public class Test {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        System.out.println("請輸入新員工的姓名:");        String name = scan.nextLine();        System.out.println("請輸入新員工應聘的程式設計語言:");        String language = scan.nextLine();        switch(language.hashCode()) {        case 3254818:        case 2301506:        case 2269730:            System.out.println("員工" + name + "被分配到Java程式開發部門。");            break;        case 3104:        case 2112:            System.out.println("員工" + name + "被分配到C#項目維護組。");            break;        case -709190099:        case 955463181:        case 9745901:            System.out.println("員工"+name+"被分配到Asp.net程式測試部門。");            break;        default:            System.out.println("本公司不需要" + language + "語言的程式開發人員。");        }    }}
迴圈 迴圈語句 - while
public class Test {    public static void main(String[] args) {        int x = 1;        int sum = 0;        while (x<=10) {            sum = sum+x;            x++;        }        System.out.println("從1到10的和是:" + sum);    }}
迴圈語句 - do…while
迴圈體,至少執行一次。
public class Test {    public static void main(String[] args) {        int a = 100;        while (a==60) { //一遍不執行            System.out.println("Ok! a==60");            a--;        }        int b = 100;        do {    //至少執行一次            System.out.println("Ok! b==60");            b--;        }        while (b==60);    }}
迴圈語句 - for
public class Test {    public static void main(String[] args) {        int sum = 0;        for(int i=1; i<=100; i+=1) {            sum = sum+i;        }        System.out.println("1到100之間數的和為:" + sum);    }}
迴圈語句 -foreach
public class Test {    public static void main(String[] args) {        int arr[] = {7, 10, 1};        System.out.println("一維數組中的元素分別為:");        for (int x : arr) {            System.out.print(x+"\t");        }    }}
執行個體 - 使用while迴圈遍曆數組
public class Test {    public static void main(String[] args) {        String[] aves = new String[] {"白露", "丹頂鶴", "黃鸝", "烏鴉", "喜鵲", "布谷鳥", "灰文鳥", "百靈鳥"};        int index = 0;        System.out.println("我的花園裡有很多鳥,種類有:");        while (index < aves.length) {            System.out.print(aves[index++] + "   ");        }    }}
執行個體 - 使用for迴圈輸出九九乘法表
public class Test {    public static void main(String[] args) {        for (int i=1; i<=9; i++) {            for (int j=1; j<=i; j++) {                System.out.print(j + "*" + i + "=" + (i*j) + "\t");            }            System.out.println();        }    }}
跳躍陳述式 - break
public class Test {    public static void main(String[] args) {        int sum = 0;        String flag = "從1到100之間連續整數的和是:";        for (int i=1; i<=100; i++) {            sum +=i;            if(sum>1000) {                flag = "從1到" + i + "之間連續整數的和是:";                //跳出該層迴圈                break;            }        }        System.out.println(flag + sum);    }}
跳躍陳述式 - continue
public class Test {    public static void main(String[] args) {        int i = 0;        System.out.println("10以內的奇數有:");        while (i<10) {            i++;            if (i%2 == 0) {                continue;            }            System.out.print(i + "  ");        }    }}
跳躍陳述式 - return
 執行個體 - 終止迴圈體(break) 
public class Test {    public static void main(String[] args) {        System.out.println("\n----中斷單層迴圈的例子----");        String[] array = new String[] {"白露","丹頂鶴","黃鸝","鸚鵡","烏鴉","喜鵲","老鷹","布谷鳥","老鷹","灰文鳥","老鷹","百靈鳥"};        for (String string : array) {            if (string.equalsIgnoreCase("老鷹"))                break;            System.out.print("有:" + string + "   ");        }        System.out.println("\n\n----中單2層迴圈的例子----");        int[][] myScores = new int[][] {{67,78,63,22,66}, {55,68,78,95,44},{95,97,92,93,81}};        System.out.println("寶寶這次考試成績:\n數學\t語文\t英語\t美術\t曆史");        No1: for (int[] is : myScores) {            for (int i : is) {                System.out.print(i + "\t");                if(i<60) {                    System.out.println("\n等等," + i + "分的是什麼。這個為什麼不及格。");                    break No1;                }            }            System.out.println();        }    }}
執行個體 - 迴圈體的過濾器(continue)
public class Test {    public static void main(String[] args) {        String[] array = new String[] {"白露","丹頂鶴","黃鸝","鸚鵡","烏鴉","喜鵲","老鷹","布谷鳥","老鷹","灰文鳥","老鷹","百靈鳥"};        System.out.println("在我的花園裡有很多鳥類,但是最近來了幾隻老鷹,請幫我把他們抓走。");        int eagleCount = 0;        for(String string : array) {            if(string.equals("老鷹")) {                System.out.println("發現一隻老鷹,已經抓到籠子裡。");                eagleCount++;                continue;            }            System.out.println("搜尋鳥類,發現了:" + string);        }        System.out.println("一共捉到了:" + eagleCount + "只老鷹。");    }}
執行個體 - 使用for迴圈輸出空心的菱形
public class Test {    public static void main(String[] args) {        printHollowRhombus(10);    }    public static void printHollowRhombus(int size) {        if (size%2==0)             size++;        for (int i=0; i<size/2+1; i++) {            for(int j=size/2; j>i+1; j--) {                System.out.print(" ");            }            for (int j=0; j<2*i+1; j++) {                if(j==0 || j==2*i)                     System.out.print("*");                else                    System.out.print(" ");            }            System.out.println("");        }        for(int i=size/2+1; i<size; i++) {            for(int j=0; j<i-size/2; j++) {                System.out.print(" ");            }            for(int j=0; j<2*size-1-2*i; j++) {                if(j==0 || j==2*(size-i-1))                    System.out.print("*");                else                    System.out.print(" ");            }            System.out.println("");        }    }}
執行個體 - 使用for迴圈輸出楊輝三角
public class Test {    public static void main(String[] args) {        int triangle[][] = new int[8][];        for (int i=0; i<triangle.length; i++) {            triangle[i] = new int[i+1];            for(int j=0; j<=triangle[i].length-1; j++) {                if(i==0||j==0||j==triangle[i].length-1)                    triangle[i][j] = 1;                else                    triangle[i][j] = triangle[i-1][j] + triangle[i-1][j-1];                System.out.print(triangle[i][j] + "\t");            }            System.out.println();        }    }}

聯繫我們

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