在这个乘法游戏中,我必须生成两个随机数并将它们相乘。用户必须猜测正确的产品。游戏结束后,用户可以选择重新启动游戏或退出(以及其他显示/重置统计信息的选择)。我需要在游戏结束后使用开关盒结构进行用户选择。我也知道我必须使用do-while循环来重启/退出游戏,但是我不知道该用什么代替粗体注释(在案例1和3之后)。在此先感谢您抽出宝贵的时间阅读。非常感谢您的帮助!
#include <stdio.h>
#include <stlib.h>
#include <time.h>
#include <math.h>
int main () {
//Start do-while loop
do {
//Display rules of the game to the user.
printf("Two random numbers (1-12) will be generated and displayed. The objective of this game is to correctly guess the product of the numbers.");
//Generate two random integers between 1 and 12 and display them to the user
int i;
int n1;
int n2;
srand(time(NULL));
for(i = 1; i<=12;i++)
{
n1 = 1 + rand() % 12;
n2 = 1 + rand() % 12;
printf("The two random numbers generated are : %d and %d\n", n1,n2);
}
//Prompt the user to enter the product of the two numbers
int a;
printf("Enter the product of the two numbers: ");
scanf("%d", &a);
//Determine and display if or not the answer was right
int countCorrect;
int countIncorrect;
int product = n1*n2;
if (a == product)
{
printf("Correct response!");
countCorrect++;
}
else
{
printf("Incorrect response, the correct answer is: %d\n", product);
countIncorrect++;
}
//Start switch-case structure for post-game options
int choice;
switch (choice)
{
case 1:
printf("You have chosen to play again");
**//How do I restart the program so the user can play again?**
break;
case 2:
printf("You have chosen to see statistics");
printf("The number of correct answers are: %d\n", countCorrect);
printf("The number of incorrect answers are: %d\n", countIncorrect);
break;
case 3:
printf("You have chosen to reset statistics");
countCorrect = 0;
countIncorrect = 0;
break;
case 4:
printf("You have chosen to quit");
**//How do I quit the program?**
break;
default:
printf("Invalid number! Please enter a number from 1 to 4.");
break;
}
}
尽管由于使用意大利面条代码,这是一种不好的做法,但在这种情况下,您始终可以使用GOTO标签。我认为在实践中,最好使用条件如下的正常while循环:
while(!gameOver)
{
//做东西
//退出循环
case x:
gameOver = true;
break;
}
您也许还可以继续执行以重新开始循环; ,它总是开始循环的下一次迭代。
需要修复的几件事。声明变量时,最好在清算时将其初始化。而不是int a; a = x;
并且仅在需要时声明变量。但这显然可以归结为偏好和实践。在C89中,变量必须在作用域的顶部声明,但是我可以告诉您您不是在编译C89。您也不需要循环兰德12次。如果要两个独立的整数,只需两个调用就足够了。请记住,兰德不是很好,但可以练习。在print语句后放置新行可以使输出更整洁。声明了countCorrect
和countIncorrect
,但未初始化为0,然后又递增。这是不好的做法,因为您不知道任何一个变量的初始值,也无法获得准确的计数。我假设您只想在用户输入4时退出,否则继续循环吗?将开关放在循环外部,然后在用户猜测产品之后,从用户那里读取选择,并在do while循环结束时使用该值。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int guess(){
int x = 1+rand()%12;
int y = 1+rand()%12;
printf("The two random numbers generated are : %d and %d\n", x,y);
printf("Enter the product of the two numbers: ");
int z = 0;
scanf("%d", &z);
if(z == (x*y)){
printf("Correct response!\n");
return 1;
}
printf("Incorrect response, the correct answer is: %d\n", x*y);
return 0;
}
int main () {
srand(time(NULL));
printf("Two random numbers (1-12) will be generated and displayed. The objective of this game is to correctly guess the product of the numbers.\n");
int correct = 0;
int incorrect = 0;
int choice = 1;
do{
if(choice==1){
if(guess()){
++correct;
}else{
++incorrect;
}
}
printf("Enter 1 to play again\nEnter 2 to see statistics\nEnter 3 to reset statistic\nEnter 4 to quit:");
scanf("%d",&choice);
switch (choice) {
case 1:
break;
case 2:
printf("You have chosen to see statistics\n");
printf("The number of correct answers are: %d\n", correct);
printf("The number of incorrect answers are: %d\n", incorrect);
break;
case 3:
printf("You have chosen to reset statistics\n");
correct = 0;
incorrect = 0;
break;
case 4:
printf("You have chosen to quit\n");
break;
default:
printf("Invalid number! Please enter a number from 1 to 4.\n");
break;
}
}while(choice !=4);
return 0;
}