天天看點

Java 基礎 之 do while 循環

http://www.verejava.com/?id=16992623980921
/**
 do{ }while(判斷表達式)  : 直到 while 判斷表達式為 false 退出循環
*/

import java.util.Scanner;

public class Test1 {

    public static void main(String[] args) {
        // 模拟切水果遊戲, 鍵盤輸入 1:西瓜,2:香蕉,3:水蜜桃,-1:雷
        //輸入數字列印對應的水果,  如果輸入-1 終止遊戲輸入

        Scanner in = new Scanner(System.in);
        int num = 0; //定義了一個變量
        do {
            num = in.nextInt();//變量的生命周期

            if (num == 1) {
                System.out.println("您切到了西瓜");
            } else if (num == 2) {
                System.out.println("您切到了香蕉");
            } else if (num == 3) {
                System.out.println("您切到了水蜜桃");
            } else if (num == -1) {
                System.out.println("您切到了 雷 遊戲終止");
            }
        } while (num != -1); //如果輸入-1 終止遊戲輸入

    }
}