猜数字小游戏


猜数字小游戏


import java.util.Scanner;

public class numberGame {
     
    public static void main(String[] args) {
     
        //小练习,用于生成大于0.5的随机数
        /*
        double randNum = 0;
        //生成一个大于0.5的随机数,只有随机数大于0.5,程序的循环才会退出

        while (randNum < 0.5) {
            //使用Java中的Math.random生成随机数
            randNum = Math.random();
            System.out.println(randNum);
        }
        System.out.println("生成的大于0.5的随机数是" + randNum);

         */

        boolean startGame = true;
        int correctGuessTotal = 0;
        int totalGameCount = 0;
        //使用while循环确定开始游戏
        while (startGame) {
     
            //使用输入函数
            Scanner in = new Scanner(System.in);
            //定义猜数范围
            int rangeStart = 30;
            int rangEnd = 50;
            //设定允许猜测次数每次游戏
            int guessTotal = 5;

            int mod = rangEnd - rangeStart;
            //检验猜数开始值和结束值是否为0
            if (rangeStart < 0 || rangEnd < 0) {
     
                System.out.println("开始值和结束值不可为0");
            }
            //用两数之模检验两数之间可以生成大于等于1个随机数
            if (mod <= 1) {
     
                System.out.println("非法的数字范围:(" + rangeStart + "," + rangEnd + ")");
            }

            //生成随机数

            int bigRandom = (int) (Math.random() * (rangEnd * 100));

            int numberToGuess = (bigRandom % mod) + rangeStart;
            //检验目标数字不小于或大于起始值和结束值,否则置为起始值加一或者结束值减一
            if (numberToGuess <= rangeStart) {
     
                numberToGuess = rangeStart + 1;
            } else if (numberToGuess > rangEnd) {
     
                numberToGuess = rangEnd - 1;
            }

            //下行注释用于检测程序使用,得出目标值numberToGuess
            //System.out.println("mod=" + mod + "\t" + "bigRandom=" + bigRandom + "\tnumberToGues=" + numberToGuess);

            int leftGuess = guessTotal;
            //定义初始正确猜测布尔值为false
            boolean correctGuess = false;

            System.out.println("请输入猜测数字,范围是【" + rangeStart + "," + rangEnd + ")");

            //游戏主体
            while (leftGuess > 0) {
     

                System.out.println("请输入猜测数字,剩余猜测次数为:" + leftGuess + ",如需退出游戏请输入“-1”");

                int guess = in.nextInt();

                //检测到负数,退出游戏主体
                if (guess < 0) {
     
                    System.out.println("用户选择退出游戏!");
                    //游戏进行次数统计放在了后面,导致-1退出也计算游戏次数,通过自减操作抵消循环外多余的自增操作
                    totalGameCount--;

                    //如果已经猜测了几个,在五次之前不想继续猜了,也统计总共游戏次数
                    if (leftGuess < 4) {
     
                        totalGameCount++;
                    }
                    break;
                }

                //进行到此步游戏正式开始,开始计算剩余次数,剩余次数减一
                leftGuess--;


                //输入的数字超出范围哦,重新开始游戏主体循环
                if (guess < rangeStart || guess > rangEnd) {
     
                    System.out.println("请输入范围在【" + rangeStart + "," + rangEnd + ")内的数字!");
                    continue;
                }

                //判断给出数字和目标数字
                if (guess == numberToGuess) {
     
                    System.out.println("恭喜猜对了!");
                    //猜对数字,正确猜测布尔值职位true,并对正确猜测次数加一
                    correctGuess = true;
                    correctGuessTotal++;
                    break;
                } else if (guess > numberToGuess) {
     
                    System.out.println("待猜数字小于" + guess);
                } else {
     
                    System.out.println("待猜数字大于" + guess);
                }

            }

            //游戏次数统计,每循环一次游戏主体,游戏次数加一
            totalGameCount++;

            //正确猜测未经过重新赋值认为false,!correctGuess为true,执行本循环,告知猜测失败和真实数字
            if (!correctGuess) {
     
                System.out.println("本局猜测失败,猜测数字实际为:" + numberToGuess);
            }

            System.out.println("游戏结束!是否继续游戏?true/false");


            //通过获取到的布尔值来为游戏是否开始赋值
            startGame = in.nextBoolean();


        }
        System.out.println("本局游戏一共进行了" + totalGameCount + "次," + "其中正确猜对" + correctGuessTotal + "次");
    }
}


你可能感兴趣的:(Java,java,游戏)