任务6、猜数小游戏

一、猜数小游戏(单次版)

  • 游戏规则:程序产生一个[1, 100]之间的随机整数,用户输入整数进行猜测,如果猜对了,就结束游戏;如果猜错了,程序会提示你是猜高了还是猜低了,然后让你继续输入整数进行猜测。

(一)编程实现

  • 代码如下:
package p02.t06;

import java.util.Random;
import java.util.Scanner;

/**
 * 功能;猜数小游戏
 * 作者:刘金花
 * 日期:2022 年06月07日
 */
public class Task06 {
    public static void main(String[] args) {
        // 声明部分
        int x, target;
        Scanner sc = new Scanner(System.in);
        Random random = new Random();

        // 产生猜测目标
        target = random.nextInt(100);
        // 输入猜测的整数
        System.out.print("输入一个[1, 100]的整数:");
        x = sc.nextInt();
        while (x != target) {
            // 判断是猜高了还是猜低了
            if (x > target) {
                System.out.println("朋友,你猜高了~继续猜吧");
            } else {
                System.out.println("朋友,你猜低了~继续猜吧");
            }
            // 继续输入猜测的整数
            System.out.print("输入一个[1, 100]的整数:");
            x = sc.nextInt();
        }

        System.out.println("恭喜你猜对了,游戏结束~");
    }
}

  • 运行程序,查看结果:
    请添加图片描述
  • 如果采用二分法来猜测,会是什么
  • 代码如下:
package p02.t06;

import java.util.Random;
import java.util.Scanner;

/**
 * 功能;猜数小游戏
 * 作者:刘金花
 * 日期:2022 年04月14日
 */
public class Task06_ {
    public static void main(String[] args) {
        //声明部分
        int target, x;
        Scanner sc = new Scanner(System.in);

        // 产生猜测目标
        Random random = new Random();
        target = random.nextInt(100);

    while (true) {
        // 用户输入猜测数
        System.out.print("输入一个[1~100]的整数:");
        x = sc.nextInt();
        // 判断用户是否猜测正确
            if (x > target) {
                System.out.println("朋友,你猜高了,继续猜吧~");
            } else if (x <target) {
                System.out.println("朋友,你猜低了,继续猜吧~");
            } else {
                break; // 猜中了,跳出循环
            }
        }

        System.out.println("恭喜,你猜对了,游戏结束!");
    }
}

  • 运行代码,查看结果:
    请添加图片描述

(二)知识点讲解:

1、测试条件循环 - while循环

(1)语法格式
初始条件
while (循环条件) {
     语句块
     break|continue;
     更新条件
}

(2)执行情况

请添加图片描述

  • 首先判断循环条件是真还是假,如果是真的,执行循环体,然后再次判断循环条件。如果是真的,继续循环,直到循环条件变成假的。有一种特殊情况:如果第一次判断循环条件就不成立,那么一次循环也不执行。

2、后测试条件循环 - do…while循环

(1)语法格式
初始条件
do {
     语句块
     break|continue;
     更新条件
} while (循环条件);

(2)执行情况
  • 如下图所示:
    请添加图片描述
  • 首先执行一次循环,然后判断循环条件,如果为真,继续循环,直到条件为假时结束循环。后测试当型循环,属于先上车后买票,无论如何都会执行一次循环。

3、随机类 - Random

  • 产生指定范围的随机整数[a,b]
Random random = new Random();
int x = a + random.nextInt(b - a);

(三)拓展练习

任务1、猜数小游戏(多次版)

  • 如果用户猜对了,会询问用户是否继续玩

任务2、构建可进可退的多级菜单系统

  • 代码如下:
package p01.t02;

import java.util.Scanner;

/**
 * 功能;构建可进可退的多级菜单
 * 作者:刘金花
 * 日期:2022 年05月26日
 */
public class XExercise02_ {
    public static void main(String[] args) {
        //声明部分
        int mc1, mc2;
        Scanner sc = new Scanner(System.in);
        boolean isRunning = true; // 循环控制变量


        while (isRunning) {
            // 绘制一级菜单
            System.out.println("名片管理系统");
            System.out.println("==================");
            System.out.println("     1.登录");
            System.out.println("     0.退出");
            System.out.println("==================");
            System.out.print("输入菜单选项[1,0]:");
            mc1 = sc.nextInt();
            // 根据用户选择执行相应操作
            switch (mc1) {
                case 1:
                    // 调用登录方法
                    login();
                    break;
                case 0:
                    System.out.println("谢谢使用~再见~");
                    isRunning = false;
                    break;
                default:
                    System.out.println("输入的菜单编号有误!");
            }
        }
    }

    /**
     * 用户登录方法
     */
    private static void login() {
        String username,password;
        Scanner sc = new Scanner(System.in);

        System.out.print("用户名:");
        username = sc.next();
        System.out.print("密 码:");
        password = sc.next();

        if (username.equals("Hflower") && password.equals("123456")) {
            System.out.println("登录成功~");
            // 名片管理方法
            cardManagement();
        } else {
            System.out.println("用户们或密码有误,登录失败~");
        }
    }

    /**
     * 名片管理方法
     */

    private static void cardManagement() {
        int mc2;
        Scanner sc = new Scanner(System.in);
        boolean isRunning = true; // 循环控制变量

        while (isRunning) {
            // 绘制二级菜单
            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("输入菜单编号[1,2,3,4]:");
            mc2 = sc.nextInt();

            switch (mc2) {
                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:
                    isRunning = false;
                default:
                    System.out.println("输入的菜单编号有误!");

            }
        }
    }
}


  • 运行程序,查看结果:
    请添加图片描述

任务3、模拟微信拼手气红包程序产生指定个数的随机红包

  • 输入总金额与红包数,随机给每个红包分配金额
  • 代码如下:
package p01.t04;

import java.util.Random;
import java.util.Scanner;

/**
 * 功能;拼手气红包
 * 作者:刘金花
 * 日期:2022 年06月08日
 */
public class redbags {
    public static void main(String[] args) {
        //amount:总金额
        // count:红包个数
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入红包总金额(元):");
        double amount = sc.nextDouble();
        System.out.print("请输入红包个数(个 ):");
        int count = sc.nextInt();
        double min = 0.01;   //红包最小金额
        Random c = new Random();   //随机数
        int x = 1;
        while (x == 1) {
            if (amount / count == min) {
                for (int i = 1; i <= count; i++) {
                    System.out.println("第" + i + "个人抢到红包:" + 0.01 + "元");
                    x = 0;
                }
            } else if (amount / count > 0.01) {
                for (int i = 1; i < count; i++) {
                    //在保证后count - i个人都得到最小红包金额0.01元的时候,前面i个人总共分的钱的总数
                    double max = amount - (count - i) * min;
                    double bound = max - min;
                    double safe = (double) c.nextInt((int) (bound * 100)) / 100;
                    //nextInt(int n)返回0~n之间的随机值,n取不到
                    double money = safe + min;//以防出现0值
                    amount = amount - money;//发出一个红包后剩下的总金额
                    System.out.println("第" + i + "个人抢到红包" + String.format("%.2f", money) + "元");
                }
                System.out.println("第" + count + "个人抢到红包" + String.format("%.2f", amount) + "元");
                x = 0;
                System.out.println("红包已被抢光,谢谢参与!");
            }
        }
    }
}

  • 运行代码,查看结果:
    请添加图片描述
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值