猜数游戏2(续集)-----选择难度

难度:中等

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <time.h>
using namespace std;
bool l(int a,int n){
	if(a<n){
	cout<<"The number of guesses is greater than the number of outputs"<<endl;
    }
	if(a>n)
	cout<<"The number of guesses is less than the number of outputs"<<endl;
	return 0;
}
bool f(int a,int n){
	if(a==n)
	return 1;
	else
	return 0;
}
int main(){
cout<<"Please Select Difficulty Level"<<endl;//请选择难度 
cout<<"1. Easy" <<endl;//1.简单 
cout<<"2. Difficulty"<<endl;//2.困难
cout<<"3. Hell" <<endl;//3.地狱
int a1,timer=30,n,a;
srand(time(0));
cin>>a1;
cout<<"Okay, I get it"<<endl;
if(a1==1){
timer=30;
n=rand()%50+1;
cout<<"Your difficulty is simple, the number of fault tolerance is 30, the number of guesses is 1 to 50"<<endl;
}
else
if(a1==2){
timer=15;
n=rand()%150+1;
cout<<"Your difficulty is difficulty, fault tolerance is 15, the number of guesses is 1 to 150"<<endl;
}
else
if(a1==3){
timer=10;
n=rand()%250+1;
cout<<"Your difficulty is hell, the number of fault tolerance is 10, the number of guesses is 1 to 250"<<endl;
}
cout<<"So ";
cout<<"The guessing game starts now!"<<endl; 
for(int i=1;;i++){
	cout<<"Please output a number"<<endl;
	cin>>a;
	if(timer-i==0){
		cout<<"The game failed"<<endl;
		cout<<"Don't lose heart"<<endl;
		break;
	}
	if(f(a,n)==1)
	{
	    cout<<"Guessed right!"<<endl;
		cout<<"Used "<<i<<" times";
		break; 
	}
	else 
	l(a,n);
	cout<<"You have "<<timer-i<<" chances left"<<endl;
	}
	return 0;
} 

解析

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <time.h>
using namespace std;
bool l(int a,int n){
	if(a<n){
	cout<<"The number of guesses is greater than the number of outputs"<<endl;
    }
	if(a>n)
	cout<<"The number of guesses is less than the number of outputs"<<endl;
	return 0;
}
bool f(int a,int n){
	if(a==n)
	return 1;
	else
	return 0;
}
int main(){
cout<<"Please Select Difficulty Level"<<endl;//请选择难度 
cout<<"1. Easy" <<endl;//1.简单 
cout<<"2. Difficulty"<<endl;//2.困难
cout<<"3. Hell" <<endl;//3.地狱
int a1,timer=30,n,a;
srand(time(0));
cin>>a1;
cout<<"Okay, I get it"<<endl;
if(a1==1){//对难度进行判断得出可猜的次数与范围
timer=30;
n=rand()%50+1;
cout<<"Your difficulty is simple, the number of fault tolerance is 30, the number of guesses is 1 to 50"<<endl;
}
else
if(a1==2){
timer=15;
n=rand()%150+1;
cout<<"Your difficulty is difficulty, fault tolerance is 15, the number of guesses is 1 to 150"<<endl;
}
else
if(a1==3){
timer=10;
n=rand()%250+1;
cout<<"Your difficulty is hell, the number of fault tolerance is 10, the number of guesses is 1 to 250"<<endl;
}
cout<<"So ";
cout<<"The guessing game starts now!"<<endl; 
for(int i=1;;i++){
	cout<<"Please output a number"<<endl;
	cin>>a;
	if(timer-i==0){//判断失败情况
		cout<<"The game failed"<<endl;
		cout<<"Don't lose heart"<<endl;
		break;
	}
	if(f(a,n)==1)
	{
	    cout<<"Guessed right!"<<endl;
		cout<<"Used "<<i<<" times";
		break; 
	}
	else 
	l(a,n);
	cout<<"You have "<<timer-i<<" chances left"<<endl;
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值