循环结构练习

青鸟迷你游戏平台开发

   1.选择游戏 ----- switch选择结构

public class Test {
    public static void main(String[] args) {
        System.out.println("欢迎进入青鸟游迷你戏平台");
        Scanner input = new Scanner(System.in);
        System.out.println("\n请选择您喜爱的游戏:\n   ");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * ");
        System.out.println("           1.斗地主");
        System.out.println("           2.斗牛");
        System.out.println("           3.泡泡龙");
        System.out.println("           4.连连看");
        System.out.println("* * * * * * * * * * * * * * * * * * * * *  ");
        System.out.print("\n请选择,输入数字: ");
        int num = input.nextInt();
        switch (num) {
            case 1:
                System.out.println("您已进入斗地主房间!");
                break;
            case 2:
                System.out.println("您已进入斗牛房间!");
                break;
            case 3:
                System.out.println("您已进入泡泡龙房间!");
                break;
            case 4:
                System.out.println("您已进入连连看房间!");
                break;
            case 5:
                main(args);
                break;
            default:
                System.out.println("对不起,您的输入错误");
                break;
        }
    }
}

 2.玩游戏并晋级 ------ 多重if /  break

public class Jinji {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = 1;
        int count = 0;           //计数
        int score = 0;          //游戏成绩
        String answer;
        System.out.println("青鸟迷你游戏平台>游戏晋级\n");
        do {
            System.out.print("您正在玩第" + n + "局,成绩为:");
            score = input.nextInt();         //录入游戏得分
            if (score > 80) {              //统计80分以上的局数
                count++;
            }
            n++;
            if (n > 5) {
                System.out.println("游戏结束");
            } else {
                System.out.print("继续玩下一局吗?(yes/no)  ");
                answer = input.next();
                if (answer.equals("no")) {                     
                    System.out.println("您已经中途退出游戏");            
                    break;
                } else {
                    System.out.println("进入下一局");
                }
            }
        } while (n <= 5);
        double rate = count / 5.0;          //计数达到80分以上的比率
        if (n > 5) {
            if (rate > 0.8) {
                System.out.println("\n恭喜通过一级");
            } else if (rate > 0.6) {
                System.out.println("\n通过二级,继续努力!");
            } else {
                System.out.println("\n对不起,您未能晋级,继续加油啊!");
            }
        } else {
            System.out.println("\n对不起,您未能晋级,继续加油啊!");
        }
    }
}

 3.玩游戏并支付游戏币 ----if结构、continue语句

public class Count {
    public static void main(String[] args) {
        System.out.println("青鸟迷你游戏平台>游戏币支付");
        Scanner input=new Scanner(System.in);
        System.out.println("请选择您玩的游戏类型:  ");
        System.out.println("            1.牌类");
        System.out.println("            2.休闲竞技类");
        int num=input.nextInt();
        System.out.println("请输入您游戏时长:  ");
        int time=input.nextInt();
        switch(num) {
            case 1:
              if (time>10) {
                 System.out.println("您玩的是牌类游戏,时长是:" + time + "小时,可以享受5折优惠,您需要支付" + (0.5 * time * 10) + "个游戏币");
              } else {
                 System.out.println("您玩的是牌类游戏,时长是:" + time + "小时,可以享受8折优惠,您需要支付"+ (0.8 * time * 10) + "个游戏币");
              }
              break;
            case 2:
              if (time<= 10) {
                 System.out.println("您玩的是休闲竞技类游戏,时长是:" + time + "小时,可以享受8折优惠,您需要支付" + (0.8 * time * 20) +"个游戏币");
              } else {
                 System.out.println("您玩的是休闲竞技类游戏,时长是:" + time + "小时,可以享受5折优惠,您需要支付" + (0.5 * time * 20) + "个游戏币");
              }
              break;
        }
        System.out.println("游戏结束");
    }
}

 4.添加用户信息

public class  User{
    public static void main(String[] args) {
        System.out.println("青鸟迷你游戏平台>添加用户信息\n");
        int zs ;//整数
        int age ;//年龄
        int score ;//积分
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要录入用户的数量`:  ");
        int count = input.nextInt();
        for (int i = 0; i < count; i++) {
            System.out.println("请输入用户编号(<4位整数>):  ");
            zs = input.nextInt();
            if (zs<=9999&&zs>=1000) {
            }else{
                System.out.println("输入有误");
                continue;
            }
            System.out.println("请输入用户年龄:   ");
            age = input.nextInt();
            if (age <= 10 || age > 100) {
                System.out.println("很抱歉,您的年龄不适宜玩游戏");
                System.out.println("录入信息失败\n");
                continue;
            }
            System.out.println("请输入会员积分:  ");
            score = input.nextInt();
            System.out.println("您录入的会员信息是:\n");
            System.out.println("用户编号:" + zs + "    年龄:" + age 
+ " 积分:" + score + "\n"); } } }

 注:灵活使用continue与break 

 

猜你喜欢

转载自www.cnblogs.com/yun---meng/p/12723134.html