猜数小游戏

游戏1

规则:在1-100之间随机生成一个数,然后你输入一个数,根据你输入的数的大小和随机数的大小进行判断。

(1)if....else方法 

package cn.itcast.day;
import java.util.Scanner;
public class Demo9 {
    public static void main(String[] args) {
        //生成1-100之间随机数
        int num = (int) (Math.random() * 100) + 1;
        Scanner sc = new Scanner(System.in);
        int guessNum = -1;
        while (guessNum != num) {
            System.out.println("请输入1-100之间整数");
            guessNum = sc.nextInt();
            if (guessNum == num) {
                System.out.println("中啦");
            } else if (guessNum < num) {
                System.out.println("小啦");
            } else {
                System.out.println("大了");
            }
        }
    }
}

运行结果为

请输入1-100之间整数
66
大了
请输入1-100之间整数
50
大了
请输入1-100之间整数
29
大了
请输入1-100之间整数
20
大了
请输入1-100之间整数
10
小啦
请输入1-100之间整数
15
小啦
请输入1-100之间整数
18
中啦

Process finished with exit code 0

(2)do...while循环

package cn.itcast.day;
import java.util.Scanner;
public class Demo9 {
    public static void main(String[] args) {
        // 记录用户输入的数字
        int guess = -1;
        // 记录用户输入次数
        int count = 0;
        // 生成1-100之间随机数
        int num = (int) (Math.random() * 100) + 1;
        Scanner sc = new Scanner(System.in);
        // 循环猜数字
        do {
            System.out.println("请输入1-100之间的数字");
            guess = sc.nextInt();
            if (guess > num) {
                System.out.println("哥们,太大了");
            } else if (guess < num) {
                System.out.println("哥们,太小了");
            } else {
                System.out.println("恭喜,中啦");
            }
            count++;
        } while (num != guess);
        System.out.println("你猜测的数字是:" + num + "猜测了" + count + "次");
    }
}

运行结果为

 


请输入1-100之间的数字
25
哥们,太小了
请输入1-100之间的数字
50
哥们,太大了
请输入1-100之间的数字
40
哥们,太小了
请输入1-100之间的数字
45
哥们,太小了
请输入1-100之间的数字
47
恭喜,中啦
你猜测的数字是:47猜测了5次

Process finished with exit code 0
游戏2

规则:随机生成两个1-10的整数,然后再随机生成一个z,z从0-4分别代表加减乘除和取模运算,然后迅速猜答案,系统会在两秒之内给出答案。

package cn.itcast.day;
import java.util.Scanner;
public class Demo9 {
    public static void main(String[] args) throws InterruptedException {
        // 生成随机数Math.random()生成0-1值,不包含0和1,
        //乘以10得到0和10之间的数(double类型),不包含0和10
        //强转为int,并加1得到1和10之间的数,包含1和10
        int x = (int)(Math.random()*10)+1;
        int y = (int)(Math.random()*10)+1;
        System.out.println(x);
        System.out.println(y);
        // 创建0-4随机数 0 1 2 3 4 各表示加减乘除取模
        int z = (int) (int)(Math.random()*5);
        System.out.println(z);
        switch (z) {
            case 0:
                System.out.println(x + "+" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "+" + y + "=" + (x + y));
                break;
            case 1:
                System.out.println(x + "-" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "-" + y + "=" + (x - y));
                break;
            case 2:
                System.out.println(x + "*" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "*" + y + "=" + (x * y));
                break;
            case 3:
                System.out.println(x + "/" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "/" + y + "=" + (x / y));
                break;
            case 4:
                System.out.println(x + "%" + y + "=?");
                System.out.println("哥们快猜。。。。");
                Thread.sleep(2000);
                System.out.println(x + "%" + y + "=" + (x % y));
                break;
        }
    }
}

运行结果:

 

8
8
2
8*8=?
哥们快猜。。。。
8*8=64

Process finished with exit code 0

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值