NO.26【婳骨】の猜数器

题目

原题:
小游戏: 猜数字
输入n(0<n<100), 想让程序猜到的值
如果程序没有猜中, 则提示太大/太小 并让程序接着猜测
程序通过二分法不断缩小猜测范围, 直到猜中n
例如n = 39
程序第一次猜测50, 判断数值太大
第二次猜测25, 判断数值太小
第三次猜测37 判断数值太小
43 大
40 大
38 小
39 正确
输入: n 表示被猜测值
输出: 每行输出一个整数, 表示程序当前猜测的数字
最终行输出猜测的次数
样例输入:
39
样例输出:
50
25
37
43
40
38
39
最终猜测了7次

代码

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)

void In(double* a);
int main(){
	int a;	//随机出来的数字
	double _b;	//所猜测的数字
	int b;	//所猜测的数字,整数形式
	int choice;	//检验是否退出
	system("title 【婳骨】の猜数器");
	while (1) {
		int fre = 0;	//输入次数
		srand((unsigned)time(NULL));
		a = rand() % 99 + 1;	//随机数的范围
		printf("欢迎来到【婳骨】の猜数器\n");
		printf("请输入1~99的整数,输入后按下回车以确认!\n");
		do {
			printf("来吧!猜数!\n");
			while (1) {
				In(&_b);
				b = (int)_b;
				if (b < 1 || b > 99) {
					printf("不在范围内哦");
				}
				else break;
			}
			if (b > a) {
				printf("输大了!不要停下来啊!\n");
				fre++;
			}
			else if (b < a) {
				printf("输小了!你不行啊!\n");
				fre++;
			}
		} while (b != a);
		printf("输对啦!");
		if (fre == 1) {
			printf("你确定你没作弊?\n");
		}
		if (fre >= 10) {
			printf("你这技术,啧啧\n");
		}
		if (fre >= 20) {
			printf("嗯,你输了\n");
		}
		printf("你总共试了%d次,取得了本场比赛的胜利!\n(难道还会输么??)\n", fre);
		printf("如要继续请输入0,否则输入任意键退出\n");
		choice = getch();
		if (choice == '0') {
			system("cls");
		}
		else {
			printf("谢谢使用!\n");
			break;
		}
	}
	system("pause");
	return 0;
}
void In(double* a) {	//对输入数据进行判断
	do {
		if (scanf_s("%lf", a) == 1) {
			break;
		}
		char choice;
		while ((choice = getchar()) != '\n' && choice != EOF);
		printf("输入的不是数字!\n");
	} while (scanf_s("%lf", a) != 1);
}

部分运行结果

在这里插入图片描述
在这里插入图片描述

小结

在这里插入图片描述

猜数游戏是一款常见的编程练习,其中通常包含一个计时器功能,用来增加游戏的互动性和挑战性。在Java中,你可以使用`java.util.Timer`或者`java.time`包中的`TimerTask`和`Duration`类来实现计时器。 以下是一个简单的猜数字游戏Java代码示例,包含了计时器功能: ```java import java.util.Random; import java.util.Timer; import java.util.TimerTask; import java.time.Duration; public class GuessNumberGame { private int secretNumber; private int guess; private Timer timer; private long startTime; public void startGame() { // 生成随机数作为秘密数字 secretNumber = new Random().nextInt(100) + 1; System.out.println("欢迎来到猜数字游戏,你有60秒的时间猜测一个1到100之间的数字。"); // 初始化计时器 timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { if (guess < secretNumber) { System.out.println("太小了,你还有59秒(当前剩余时间)"); } else if (guess > secretNumber) { System.out.println("太大了,你还有59秒(当前剩余时间)"); } else { System.out.println("恭喜你,你猜对了!用时" + Duration.between(startTime, System.currentTimeMillis()).toSeconds() + "秒。"); endGame(); } } }, 0, 1000); // 每隔1秒检查一次,总共60秒 startTime = System.currentTimeMillis(); // 记录开始时间 askForGuess(); } private void askForGuess() { try { guess = Integer.parseInt(System.console().readLine("请输入你的猜测: ")); } catch (Exception e) { System.out.println("输入无效,请输入一个整数。"); askForGuess(); } } private void endGame() { timer.cancel(); // 停止计时器 timer.purge(); // 清理已取消的任务 System.out.println("游戏结束,感谢参与!"); } public static void main(String[] args) { new GuessNumberGame().startGame(); } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值