猜数字(时间制随机)1.2.33
2024-09-08 08:12:53
发布于:浙江
#include <Windows.h>
#include <bits/stdc++.h>
long long a,b,l,r,pg,nc;
char pa = 'y';
void pga() {
system("cls");
srand(static_cast<unsigned int>(time(0)));
std::cout<<"猜数字游戏!\n"<<"请输入最小值和最大值:\n";
std::cin>>a>>b;
l=std::min(a,b);
r=std::max(a,b);
long long ng = rand() % r + l;
system("cls");
std::cout<<l<<"~"<<r<<"猜数字。\n";
while (pg != ng) {
std::cout << "请输入你的猜测:";
std::cin >> pg;
nc++;
if(pg < ng && pg > l){
std::cout << "太小了,范围在:"<<pg<<"~"<<r<<std::endl;
l = pg ;
}else if(pg > ng && pg < r){
r = pg ;
std::cout << "太大了,范围在:"<<l<<"~"<<pg<<std::endl;
}else if(pg < l || pg > r){
std::cout << "不要乱输入,再重申一遍,范围在:" << l << "~" << r << "\n";
continue;
}else{
std::cout << "恭喜你,猜对了!你一共猜了" << nc << "次。" << std::endl;
}
}
}int main(){
while (pa == 'y') {
pga();
std::cout << "你想再玩一次吗?(y继续): "<<std::endl;
std::cin >> pa;
}system("cls");
MessageBoxA(NULL, "猜数字已运行完毕", "关闭",MB_ICONWARNING);
return 0;
}
在Dev-cpp能玩。
感谢你说得对但是骗分启动提供1.1.0版本
输入不要超过2^60就不会有bug。
全部评论 3
在?
2024-08-30 来自 浙江
0不行,笑死我了没做边界判断,导致乱输数字竟然可以变成边界?
#include <Windows.h> #include <bits/stdc++.h> using namespace std; void pga() { system("cls"); int a=0,b=0,l=0,r=0; srand(static_cast<unsigned int>(time(0))); cout<<"猜数字游戏!\n"<<"请输入最小值和最大值:\n"; cin>>a>>b; l=min(a,b);r=max(a,b); int pg = 0,nc = 0,ng = rand() % r + l; system("cls"); cout<<l<<"~"<<r<<"猜数字。\n"; while (pg != ng) { cout << "请输入你的猜测:"; cin >> pg; nc++; if(pg < ng && pg > l){ cout << "太小了,范围在:"<<pg<<"~"<<r<<endl; l = pg ; }else if(pg > ng && pg < r){ cout << "太大了,范围在:"<<l<<"~"<<pg<<endl; r = pg ; }else if(pg <= l || pg >= r){ cout << "不要乱输入,再重申一遍,范围在:" << l << "~" << r << "\n"; }else{ cout << "恭喜你,猜对了!你一共猜了" << nc << "次。" << endl; } } }int main() { char pa = 'y'; while (pa == 'y') { pga(); cout << "你想再玩一次吗?(y/n): "; cin >> pa; }system("cls"); MessageBoxA(NULL, "猜数字已运行完毕", "关闭",MB_ICONWARNING); }
修改了一下加了下边界条件
2024-08-19 来自 浙江
0报错:
未命名1 In function 'int main()':
35 72 未命名1 [Error] expected '}' at end of input
28 13 未命名1 [Note] to match this '{'
C:\Users\Administrator\Desktop\未命名1.cpp In function 'int main()':
35 72 C:\Users\Administrator\Desktop\未命名1.cpp [Error] expected '}' at end of input
28 13 C:\Users\Administrator\Desktop\未命名1.cpp [Note] to match this '{'2024-08-19 来自 浙江
0缺划括号了呗,鼠标点点花括号旁边会显示对应的,感觉歪了就添一个
2024-08-19 来自 上海
0大概率你复制错了,我这里没问题
2024-08-19 来自 上海
0哦,现在好了。
2024-08-19 来自 浙江
0
有帮助,赞一个