12-27選擇結構
阿新 • • 發佈:2018-12-27
1、輸出機票價格
package com.XuanZe; /* *JiPiao.java *@Boer *2018-12-26 *輸出機票價格 */ import java.util.Scanner; public class JiPiao2 { public static void main(String[] args) { //匯入Scanner類 Scanner input = new Scanner(System.in); //定義個常量float float JIPIAO = 5000; while(true) { System.out.println("請輸入您出行的月份:1~12"); int yueFeng = input.nextInt(); System.out.println("請問您選擇的是頭等艙還是經濟艙?頭等艙輸入1,經濟艙輸入2"); int cangWei = input.nextInt(); switch(yueFeng) { case 4: case 5: case 6:case 7: case 8: case 9: case 10: double sum = cangWei == 1?JIPIAO*0.9:JIPIAO*0.6; System.out.println("您的機票價格為:"+sum); break; default: double sum1 = cangWei == 1?JIPIAO*0.5:JIPIAO*0.4; System.out.println("您的機票價格為:"+sum1); } } } }
2、骰子游戲
package com.XuanZe; import java.util.Scanner; /* * 2018-12-26 * 骰子賭博遊戲 * */ public class Game { public static void main(String[] args) { //匯入一個鍵盤輸入的類 Scanner input = new Scanner(System.in); //定義錢數 int money = 1000; System.out.println("*****歡迎來到澳門賭場*****"); while(true) { System.out.println("是否開始遊戲Y/N?"); String Y_N = input.next(); //判斷是否遊戲除了輸入N,否則視為開始遊戲 if(!"N".equals(Y_N)) { System.out.println("遊戲開始"); //定義骰子數a b c int a = (int)(Math.random()*6)+1; int b = (int)(Math.random()*6)+1; int c = (int)(Math.random()*6)+1; //定義結果daoXiao String daXiao = (a+b+c)>9?"大":"小"; //定義yaDaXiao System.out.print("請壓大小:"); String yaDaXiao = input.next(); //定義下注yaQian System.out.print("請下注:"); int yaQian = input.nextInt(); //輸出骰子結果 System.out.println("買定離手"+a+","+b+","+c+","+daXiao); if(yaDaXiao.equals(daXiao)) { System.out.println("恭喜你壓中了"); money += yaQian; }else { System.out.println("很遺憾你沒中獎"); money -= yaQian; } System.out.println("當前餘額"+money); }else { System.out.println("請退出!"); break; } } } }
3、switch實現135學習,246複習,週日休息
package com.XuanZe; import java.util.Scanner; /* * KeCheng.java * @Boer * 時間安排 * */ public class KeCheng { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("請輸入今天是星期幾:"); while(true) { int day = input.nextInt(); switch(day) { case 1: case 3: case 5: System.out.println("今天是學習!"); break; case 2: case 4: case 6: System.out.println("今天是複習!"); break; case 7: System.out.println("今天是休息!"); break; default: System.out.println("輸入不規範,請重試!"); break; } System.out.println("請輸入今天是星期幾:"); } } }
百度網盤:https://pan.baidu.com/disk/home?#/all?vmode=list&path=%2F%E4%BD%9C%E4%B8%9A