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