import java.util.Scanner;

public class GuessNumberGame { public static void main(String[] args) { int answer = (int) (Math.random() * 8) + 1; // 生成随机数 int guess = 0; // 猜测的数字 int count = 0; // 猜测次数

    Scanner scanner = new Scanner(System.in);
    System.out.println("猜数字游戏开始!请输入一个1~8之间的数字:");

    while (guess != answer) {
        guess = scanner.nextInt();
        count++;

        if (guess > answer) {
            System.out.println("猜大了,请输入一个小一点的数字:");
        } else if (guess < answer) {
            System.out.println("猜小了,请输入一个大一点的数字:");
        } else {
            System.out.println("恭喜你猜对了!你一共猜了" + count + "次。");
        }
    }
}
用java编写:随机给出一个1~8的数字然后猜是什么数字可以随便猜一个数字游戏会提示太大还是太小从而缩小结果范围经过几次猜测与提示后最终推出答案并记录你最终猜对时所需要次数。

原文地址: http://www.cveoy.top/t/topic/cAbm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录