第三章循环结构练习

1.赌骰子:

 1     package com.java;
 2     
 3     import java.util.Scanner;
 4     
 5     public class DuBo2 {
 6         public static void main(String[] args) {
 7             Scanner input = new Scanner(System.in);
 8             System.out.println("欢迎来到大赌场");
 9             System.out.println("您有多少本金?");
10             int money = input.nextInt();
11             while(true) {
12                 int a = (int)(Math.random()*6+1);
13                 int b = (int)(Math.random()*6+1);
14                 int c = (int)(Math.random()*6+1);
15                 String result =a+b+c>10?"":"";
16                 
17                 System.out.println("是否开始游戏?y/n");
18                 String chose=input.next();
19                 if(!"n".equals(chose)) {
20                     System.out.println("请下注!");
21                     int pay=input.nextInt();
22                     if(pay<=money) {
23                         System.out.println("请压大小?");
24                         String guess=input.next();
25                         System.out.println("买定离手:"+a+";"+b+";"+c+"-->"+result);
26                         if(result.equals(guess)) {
27                             System.out.println("恭喜您买中了!");
28                             money+=pay;
29                         }else {
30                             System.out.println("很遗憾您没有买中!");
31                             money-=pay;
32                         }
33                     }else {
34                         System.out.println("您下注的金额不能大于余额!");
35                         continue;
36                     }    
37                     System.out.println("您当前余额为:"+money);
38                     
39                 }else {
40                     System.out.println("搏一搏,单车变摩托!");
41                     break;
42                 }
43             if(money<=1000) {
44                 System.out.println("您当前余额不足,需要重新充值,是否充值?y/n");
45                 String chon = input.next();
46                 if(!"n".equals(chon)) {
47                     System.out.println("请输入充值金额!");
48                     int jine = input.nextInt();
49                     money+=jine;
50                     continue;
51                 }else {
52                     System.out.println("搏一搏,单车变摩托!");
53                     break;
54                 }
55             }
56             }
57             
58             
59         }
60     
61     }

输出为:

2.

 1 package com.Zuoye;
 2 
 3 
 4 
 5 import java.util.Scanner;
 6 
 7 public class Xiti1 {
 8     public static void main(String[]agrs) {
 9         Scanner input = new Scanner(System.in);
10         int num = 0;
11         int max = 0;
12         int min = 0;
13         do {
14             System.out.println("请输入一个整数(输入0时结束):");
15              num = input.nextInt();
16         if(max==0) {
17             max = num;
18         }else if(min ==0) {
19             min = num;
20         }else if(num>max&&num!=0) {
21             max = num;
22         }else if(num<min&&num!=0) {
23             min = num;
24         }
25         
26         
27         
28         }while(num!=0);
29         System.out.println("最大值为:"+max+";"+"最小值为:"+min);
30     }
31 
32 }

输出为:

复习题:
1、八大基本数据类型有哪些?
byte,short,int,long float double char boolean
2、条件运算(三目运算)的语法是什么?
条件?true:false  String result = a+b+c>=10?"大":"小";
3、选择结构的语法有哪些?
if(){}
if()else{}
if()else if(){}else{}
if(){if(){}else{}}else{}
switch(){case 常量:break;}
预习题:
1、循环结构的语法有哪些?
while(){}  do{}while(); for(){}
2、循环的几个要素有哪些?4个
循环初始值,循环条件(对初始值的判断),循环体,循环的退出
3、各种循环结构有什么特点?
while:先判断,后执行
do{}while()先执行一次,再判断
for()先判断后执行
4.break:结束所在循环,后续操作不执行,结束整个循环。
continue:跳过本次循环,后续操作不执行,进行下轮循环

原文地址:https://www.cnblogs.com/Zhangchuanfeng1/p/10199578.html