猫史档案馆


c++教程2【猜数游戏(从1的升级)

用户:捉虫捉虫查看:1 回复:5 评论:1 创建时间:2022-12-16T18:40:55


#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Start();
void GetResults();
int i, j, life, maxrand;
char c;
void Start() {
	i = 0;
	j = 0;
	life = 0;
	maxrand = 6;
	cout << "Select difficulty mode:\n";// the user has to select a difficutly level
	cout << "1 : Easy (0-15)\n";
	cout << "2 : Medium (0-30)\n";
	cout << "3 : Difficult (0-50)\n";
	cout << "or type another key to quit\n";
	c = 30;

	cin >> c;                   // read the user's choice
	cout << "\n";

	switch (c) {
		case '1':
			maxrand = 15;  // the random number will be between 0 and maxrand
			break;
		case '2':
			maxrand = 30;
			break;
		case '3':
			maxrand = 50;
			break;
		default:
			exit(0);
		break;
	}

	life = 5;         // number of lifes of the player
	srand((unsigned)time(NULL)); // init Rand() function
	j = rand() % maxrand;  // j get a random value between 0 and maxrand
	
	GetResults();
}

void GetResults() {
	if (life <= 0) { // if player has no more life then he loses
		cout << "You lose !\n\n";
		Start();
	}

	cout << "Type a number: \n";
	cin >> i;
	
	if((i>maxrand) || (i<0)) { // if the user number isn't correct, restart
		cout << "Error: number not between 0 and \n" << maxrand;
		GetResults();
	}

	if(i == j) {
		cout << "YOU WIN!\n\n"; // the user found the secret number
		Start();
	} else if(i>j) {
		cout << "Too BIG\n";
		life = life - 1;
		cout << "Lives remaining: " << life << "\n\n";
		GetResults();
	} else if(i<j) {
		cout << "Too SMALL\n";
		life = life - 1;
		cout << "Lives remaining: " << life << "\n\n";
		GetResults();
	}
}

int main() {
	cout << "** Jackpot game **\n";
	cout << "The goal of this game is to guess a number.\n";
	cout << "Jackpot will tell you if the number is too big or too\n";
	cout << "喵all compared to the secret number to find.\n\n";
	Start();
	return 0;
}

这段代码刚刚使用因为注释的,现在讲人话

#include <iostream>
#include <stdlib.h>
#include <time.h>//头文件 
using namespace std;
void Start();//可以把它想象成function函数 
void GetResults();
int i, j, life, maxrand;
char c;
/*对应第五行 void Start();*/void Start() {
	i = 0;
	j = 0;
	life = 0;
	maxrand = 6;
	
	cout << "Select difficulty mode:\n"; //用户的难度等级 
	cout << "1 : Easy (0-15)\n";//等级一:0-15(数字范围) 下同 
	cout << "2 : Medium (0-30)\n";//2:0-30
	cout << "3 : Difficult (0-50)\n";//3:0-15
	cout << "or type another key to quit\n";//或输入其他键退出
	c = 30;

	cin >> c;                   // read the user's choice
	cout << "\n";

	switch (c) {//判断所输入的数字范围 
		case '1':
			maxrand = 15;  
			break;
		case '2':
			maxrand = 30;
			break;
		case '3':
			maxrand/* 这是变量*/ = 50;
			break;
		default:
			exit(0);//退出 
		break;//停止 
	}

	life = 5;  
	srand((unsigned)time(NULL)); // init Rand() function(函数) 
	j = rand() % maxrand; 
	
	GetResults();//调用函数 
}

void GetResults() {
	if (life <= 0) { // life是玩家的生命值,一共有life次 
		cout << "You lose !\n\n";
		Start();//调用函数 
	}

	cout << "Type a number: \n";
	cin >> i;
	
	if((i>maxrand) || (i<0)) { //如果数字不在范围内 
		cout << "Error: number not between 0 and \n" << maxrand;
		GetResults();
	}

	if(i == j) {
		cout << "YOU WIN!\n\n"; // 猜对了数字 
		Start();
	} else if(i>j) {
		cout << "Too BIG\n";//数字大了 
		life = life - 1;
		cout << "Lives remaining: " << life << "\n\n";
		GetResults();
	} else if(i<j) {//数字小了 
		cout << "Too SMALL\n";
		life = life - 1;
		cout << "Lives remaining: " << life << "\n\n";
		GetResults();
	}
}

int main() {
	cout << "** Jackpot game **\n";
	cout << "The goal of this game is to guess a number.\n";
	cout << "Jackpot will tell you if the number is too big or too\n";
	cout << "喵all compared to the secret number to find.\n\n";
	Start();
	return 0;
}

你懂了吗?


回复

上一页1 页 / 共 1下一页
捉虫捉虫

其实我有很多语法和逻辑没翻译

懂的人评论区回答

 

点赞1


评论


AokenAoken

不懂(bushi(不会))

点赞0


评论


逐梦LYQ逐梦LYQ

我刚刚开始

点赞0


评论


好吃的培根好吃的培根

#include<bits/stdc++.h>
#include<cstdlib>
#include<iostream>
using namespace std;
int main()
{
int a,b,c;//a要2的数,b商,c余数(得数)
cin>>a;
for(a;b!=1;a--) {
c=a%2;
b=a/2;
a=b;
}
cout<<c;
return 0;
}

emotion_编程猫_厉害了

正好,C++

点赞0


评论


好吃的培根好吃的培根

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Start();
void GetResults();
int i, j, life, maxrand;
char c;
void Start() {
i = 0;
j = 0;
life = 0;
maxrand = 6;
cout << "Select difficulty mode:\n";// the user has to select a difficutly level
cout << "1 : Easy (0-15)\n";
cout << "2 : Medium (0-30)\n";
cout << "3 : Difficult (0-50)\n";
cout << "or type another key to quit\n";
c = 30;

cin >> c; // read the user's choice
cout << "\n";

switch (c) {
case '1':
maxrand = 15; // the random number will be between 0 and maxrand
break;
case '2':
maxrand = 30;
break;
case '3':
maxrand = 50;
break;
default:
exit(0);
break;
}

life = 5; // number of lifes of the player
srand((unsigned)time(NULL)); // init Rand() function
j = rand() % maxrand; // j get a random value between 0 and maxrand

GetResults();
}

void GetResults() {
if (life <= 0) { // if player has no more life then he loses
cout << "You lose !\n\n";
Start();
}

cout << "Type a number: \n";
cin >> i;

if((i>maxrand) || (i<0)) { // if the user number isn't correct, restart
cout << "Error: number not between 0 and \n" << maxrand;
GetResults();
}

if(i == j) {
cout << "YOU WIN!\n\n"; // the user found the secret number
Start();
} else if(i>j) {
cout << "Too BIG\n";
life = life - 1;
cout << "Lives remaining: " << life << "\n\n";
GetResults();
} else if(i<j) {
cout << "Too SMALL\n";
life = life - 1;
cout << "Lives remaining: " << life << "\n\n";
GetResults();
}
}

int main() {
cout << "** Jackpot game **\n";
cout << "The goal of this game is to guess a number.\n";
cout << "Jackpot will tell you if the number is too big or too\n";
cout << "喵all compared to the secret number to find.\n\n";
Start();
return 0;
}

点赞0


评论