循环结构作业

1.

 1 package com.ketang;
 2 /**
 3  * 输入一个不为0的整数,输出其中的最大值和最小值,输入数字0的时候结束循环。
 4  * 12.29
 5  * 
 6  */
 7 import java.util.Scanner;
 8 public class JIsuanDaXiao {
 9     public static void main(String[] args) {
10         int max ;        //先给max min定义
11         int min;
12         System.out.println("请输入一个整数(输入0结束)");
13         Scanner sc = new Scanner(System.in);
14         int c =sc.nextInt();
15         max = c;        //给max和min赋值
16         min = c;
17         while(true) {    //无线循环
18             if(c!=0) {    //不为0的情况
19                 if(c>max) {
20                     max=c;
21                 }else if(c<min) {
22                     min =c;
23                 }
24                 System.out.println("请输入一个整数(输入0结束)");
25                 c =sc.nextInt();
26             }else {        //当输入数为0的时候
27                 System.out.println("结束");
28                 break;        //强制结束
29             }    
30         }
31         System.out.println("最大值是:" + max +"最小值:" + min);
32     }
33 }

2.

 1 /**
 2  * 做一个循环的摇塞子系统,使用while
 3  * @author 程学伟
 4  *12.279
 5  */
 6 import java.util.*;
 7 public class CaoGao {
 8     public static void main(String[] args) {
 9         Scanner sc = new Scanner (System.in);
10         int money = 20000;
11         System.out.println("欢迎来到皇家赌场");
12         while(true) {
13             System.out.println("是否开始游戏:y/n");
14             String choose = sc.next();
15             if(!"n".equals(choose)) {  //选择n的情况
16                 System.out.println("请下注金额,下注金额不可大于三万或小于100");
17                 int pay = sc.nextInt();
18                 while(pay>money) {
19                     System.out.println("对不起,你余额不足,请从新选择,你现在还剩金额:"+money);
20                     System.out.println("请重新下注金额");
21                     pay = sc.nextInt();
22                     }
23                 while(pay>30000||pay<=100) { //不可下注大于三万也不可小于100
24                     System.out.println("对不起,下注金额错误");
25                     System.out.println("请重新下注金额");
26                     pay = sc.nextInt();
27                     }
28                 
29                 int a;
30                 int b;
31                 int c;
32                 String result;
33                 System.out.println("买大还是买小?");
34                 String guess =sc.next();
35                 if(money>35000) {  //当玩家的钱大于35000时
36                     while(true) {
37                     a = (int)(Math.random()*6)+1; //随机生成一个1-6的数
38                     b = (int)(Math.random()*6)+1;
39                     c = (int)(Math.random()*6)+1;
40                     result = (a+b+c)>10?"大":"小";    //3-10是小,11-18是大
41                     if(!guess.equals(result)) {  //如果猜错,则关闭循环,输出猜错的结果
42                         break;
43                         }
44                     }
45                 }else {
46                     a = (int)(Math.random()*6)+1; //随机生成一个1-6的数
47                     b = (int)(Math.random()*6)+1;
48                     c = (int)(Math.random()*6)+1;
49                     result = (a+b+c)>10?"大":"小";
50                 }
51                 
52                 System.out.println("买定离手:"+a+","+b+","+c+"——"+result);
53                 if(guess.equals(result)) {  // 猜对的情况
54                     money +=pay;
55                     System.out.println("恭喜你猜对了");
56                 }else {  //猜错的情况
57                     money -=pay;
58                     System.out.println("对不起你猜错了");
59                 }
60                 System.out.println("你现在还剩金额:"+money);
61                 
62             }else {        // 选n的情况
63                 System.out.println("正在关闭游戏");
64                 break;        // 终止循环
65             }
66         }
67         System.out.println("游戏结束");
68     }
69 }

 

猜你喜欢

转载自www.cnblogs.com/cheng1994/p/10196203.html