[英]How to use a do-while loop WITHIN A SWITCH-CASE STRUCTURE in C/C++ to restart a program from the beginning?
在这个乘法游戏中,我必须生成两个随机数并将它们相乘。 用户必须猜测正确的产品。 游戏结束后,用户可以选择重新开始游戏或退出(以及显示/重置统计数据的其他选择)。 我需要为用户在游戏后做出的选择使用 switch-case 结构。 我也知道我必须使用 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 label。 我认为在实践中最好有一个正常的while循环,条件如下:
而(!游戏结束)
{
//做东西
//退出循环
案例 x:
游戏结束 = 真;
休息;
}
您也可以通过 continue 重新启动循环; ,它总是开始循环的下一次迭代。
有几件事需要修复。 声明变量时,最好在声明时初始化它们。 而不是int a; a = x;
int a; a = x;
并且只在需要时声明变量。 但这显然可以归结为偏好和实践。 在 C89 中,变量必须在 scope 的顶部声明,但我可以告诉你没有编译 C89。 您也不需要循环 rand 12 次。 如果您想要两个单独的整数,只需两次调用就足够了。 请记住,rand 不是很好,但对于练习来说还可以。 在 print 语句后换行可以使 output 更整洁。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.