7-71 guess the number game (15 points)

7-71 Number guessing game (15 points)
The number guessing game is to make the game machine randomly generate a positive integer within 100. The user inputs a number to guess it. You need to write a program to automatically compare it with the randomly generated guessed number. Compare and prompt whether it is too big ("Too big") or small ("Too small"), equal means you guessed it. If guessed, the program ends . The program also requires counting the number of guesses. If the number is guessed once, it will prompt "Bingo!"; if the number is guessed within 3 times, it will prompt "Lucky You!"; If the number is guessed within N times (including the Nth time), it will prompt "Good Guess!"; if it has not been guessed more than N times, it will prompt "Game Over" and end the program. If the user enters a negative number before reaching N times, also output "Game Over" and end the program.

Input format:
Enter two positive integers not exceeding 100 in the first line, which are the random numbers generated by the game machine and the maximum number of guesses N. Finally, each line gives the user's input until a negative number appears.

Output format:
Output the corresponding result of each guess in one line until the output of the guessed result or "Game Over" is over.

It should be noted here: more than n times, that is, guessed n times but failed to guess. At this time, the number of times is n. If only the number of times is judged, there are two results, one is the last guess, and the other is no guess. , so it is necessary to add a judgment condition that the two numbers are not equal.
That is, the judgment condition count==con&&n!=m.

#include<iostream>
using namespace std;
/*数字游戏是令游戏机随机产生一个100以内的正整数,
用户输入一个数对其进行猜测,需要你编写程序自动对其与随机产生的被猜数进
行比较,并提示大了(“Too big”),还是小了(“Too small”),相等表示猜到了。
如果猜到,则结束程序。程序还要求统计猜的次数,如果1次猜出该数,提示“Bingo!”;
如果3次以内猜到该数,则提示“Lucky You!”;如果超过3次但是在N(>3)
次以内(包括第N次)猜到该数,则提示“Good Guess!”;如果超过N次都没有猜到,
则提示“Game Over”,并结束程序。
如果在到达N次之前,用户输入了一个负数,也输出“Game Over”,并结束程序。
*/ 
int main(){
    
    
	int m,con,n,count=0;
	cin>>m>>con;
	for(int i=0;i<con;i++){
    
    
		cin>>n;
		count++;//猜的次数
		if(n<0){
    
    
			cout<<"Game Over"<<endl;
			return 0;
		} 
		if(n>m)
			cout<<"Too big"<<endl;
		else if(n<m)
			cout<<"Too small"<<endl;
		else if(n==m){
    
    
			if(count==1)
				cout<<"Bingo!"<<endl;
			else if(count<=3)
				cout<<"Lucky You!"<<endl;
			else if(count>3&&count<=con)
				cout<<"Good Guess!"<<endl;
			return 0;
		} 
		if(count==con&&n!=m)
			cout<<"Game Over";
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45534301/article/details/112556471