第三章练习
课后第三题:
1.
1 package com.Zuoye; 2 3 import java.util.Scanner; 4 5 public class Zuoye1 { 6 public static void main(String[] args) { 7 int month = 0; 8 int num = 0; 9 int money = 5000; 10 Scanner sc = new Scanner(System.in); 11 System.out.println("请输入您出行的月份:1~12"); 12 month = sc.nextInt(); 13 switch (month) { 14 case 10: 15 case 9: 16 case 8: 17 case 7: 18 case 6: 19 case 5: 20 case 4: 21 System.out.println("请问您选择头等舱还是经济舱?头等舱请输入1,经济舱请输入2"); 22 num = sc.nextInt(); 23 if(num == 1) {System.out.println("您的机票价格为:"+money*0.9);} 24 else {System.out.println("您的机票价格为:"+money*0.6);} 25 break; 26 case 1: 27 case 2: 28 case 3: 29 case 11: 30 case 12: 31 System.out.println("请问您选择头等舱还是经济舱?头等舱请输入1,经济舱请输入2"); 32 num = sc.nextInt(); 33 if(num == 1) {System.out.println("您的机票价格为:"+money*0.5);} 34 else {System.out.println("您的机票价格为:"+money*0.4);} 35 default: 36 37 } 38 39 } 40 41 }
//switch(表达式){
//case常量1:
//语句;
//break;
//case常量2:
//语句;
//break;
//....
//default:
//语句;
//break;
//}
输出结果:
2.
用switch实现周135学习,周246复习,周日休息。
1 package com.Zuoye; 2 3 import java.util.Scanner; 4 5 public class Lianxi3 { 6 public static void main(String[] args) { 7 int time = 0; 8 Scanner sc = new Scanner(System.in); 9 System.out.println("请输入今日周几?"); 10 time = sc.nextInt(); 11 switch (time) { 12 case 1: 13 case 3: 14 case 5: 15 System.out.println("学习"); 16 17 break; 18 case 2: 19 case 4: 20 case 6: 21 System.out.println("复习"); 22 break; 23 case 7: 24 System.out.println("休息"); 25 26 27 } 28 29 30 } 31 32 }
输出结果:
3.
骰子**游戏:
1 package com.java; 2 3 import java.util.Scanner; 4 5 public class Dome4 { 6 public static void main(String[] args) { 7 int money = 1000; 8 Scanner input = new Scanner (System.in); 9 System.out.println("欢迎来到大赌场:"); 10 while(true) { 11 System.out.println("是否开始游戏:(Y/N)"); 12 String choose = input.next(); 13 if(!"N".equals(choose)){ 14 int a =(int)(Math.random()*6)+1; 15 int b =(int)(Math.random()*6)+1; 16 int c =(int)(Math.random()*6)+1; 17 String result = (a+b+c)>=10?"大":"小"; 18 System.out.println("请**:"); 19 int pay = input.nextInt(); 20 System.out.println("压大压小"); 21 String gesse = input.next(); 22 System.out.println("买定离手"+a+","+b+","+c+"----"+result); 23 if(gesse.equals(result)) { 24 System.out.println("恭喜您猜对了"); 25 money+=pay; 26 }else { 27 System.out.println("很遗憾!"); 28 money-=pay; 29 } 30 System.out.println("余额"+money); 31 }else { 32 System.out.println("离开"); 33 } 34 } 35 36 37 38 } 39 }
输出结果:
第三章预习:
1.while循环:
变量初始化
while(循环条件){
循环体
}
特点:循环条件是一个布尔表达式,它的值为布尔类型“真”或“假”。
while语句是先判断循环条件后执行循环体,如果第一次判断循环条件为假,则一次循环也不执行。
2.do-while循环:
do{循环体}while(循环条件)
特点:先执行循环体,再对循环条件进行判断,结果为真,再进行循环,结果为假,终止循环,执行后面语句。
do{循环体}while(循环条件)
特点:先执行循环体,再对循环条件进行判断,结果为真,再进行循环,结果为假,终止循环,执行后面语句。
3.for循环:
for(表达式1;表达式2;表达式3){循环体}(i=0;i<5;i++)
表达式1:实现变量的初始化;表达式2:循环条件;表达式3:修饰循环变量的值
特点:先执行表达式1,表达式2,对循环条件进行判断,满足条件进行循环体,循环语句执行完毕,改变循环变量的值,再执行表达式2,结果为真继续循环,结果为假终止循环,进行后面的语句。
for(表达式1;表达式2;表达式3){循环体}(i=0;i<5;i++)
表达式1:实现变量的初始化;表达式2:循环条件;表达式3:修饰循环变量的值
特点:先执行表达式1,表达式2,对循环条件进行判断,满足条件进行循环体,循环语句执行完毕,改变循环变量的值,再执行表达式2,结果为真继续循环,结果为假终止循环,进行后面的语句。
4.while(循环条件1){循环体1;for(循环条件2){循环体2}}
特点:外层while循环每进行一次,内层for循环从头到尾都进行一次。
特点:外层while循环每进行一次,内层for循环从头到尾都进行一次。
5.break语句:
break语句在循环中的作用是终止当前循环,在swith语句中是终止swith。
break语句在循环中的作用是终止当前循环,在swith语句中是终止swith。
6.continue语句:
作用:强制循环提前返回,让循环跳过本次循环的剩余部分,让然后开始下一次循环。
作用:强制循环提前返回,让循环跳过本次循环的剩余部分,让然后开始下一次循环。