如何在C/C+中的SWITCH-CASE结构中使用do-while循环+;从头重新启动程序?
在这个乘法游戏中,我必须生成两个随机数并将它们相乘。用户必须猜测正确的产品。游戏结束后,用户可以选择重新启动游戏或退出(以及其他显示/重置统计数据的选项)。我需要使用一个开关盒结构的选择,用户决定后的游戏。我也知道我必须使用do-while循环来重新启动/退出游戏,但是我不知道用什么来代替粗体的评论(在案例1和案例3之后)。提前感谢您抽出时间阅读。非常感谢您的帮助如何在C/C+中的SWITCH-CASE结构中使用do-while循环+;从头重新启动程序?,c,switch-statement,do-while,C,Switch Statement,Do While,在这个乘法游戏中,我必须生成两个随机数并将它们相乘。用户必须猜测正确的产品。游戏结束后,用户可以选择重新启动游戏或退出(以及其他显示/重置统计数据的选项)。我需要使用一个开关盒结构的选择,用户决定后的游戏。我也知道我必须使用do-while循环来重新启动/退出游戏,但是我不知道用什么来代替粗体的评论(在案例1和案例3之后)。提前感谢您抽出时间阅读。非常感谢您的帮助 #include <stdio.h> #include <stlib.h> #include <tim
#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;
}
}
#包括
#包括
#包括
#包括
int main(){
//启动边做边循环
做{
//向用户显示游戏规则。
printf(“将生成并显示两个随机数(1-12)。此游戏的目标是正确猜测数字的乘积。”);
//生成两个介于1和12之间的随机整数,并将其显示给用户
int i;
int-n1;
int n2;
srand(时间(空));
对于(i=1;i需要修正的几件事。在声明变量时,最好在声明变量时对其进行初始化。而不是inta;a=x;
,并且只在需要时声明变量。但这显然可以归结为偏好和实践。在C89中,变量必须在范围顶部声明,但我可以告诉你,你没有编译C89。你也不需要循环rand 12次。如果你想要两个独立的int,只需两次调用就足够了。记住rand不是很好,但为了练习,它是可以的。在print语句后放置一个新行可以使输出更整洁。countCorrect
和countCorrect
声明了,但not初始化为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;
}
#包括
#包括
#包括
int guess(){
int x=1+rand()%12;
int y=1+rand()%12;
printf(“生成的两个随机数是:%d和%d\n”,x,y);
printf(“输入两个数字的乘积:”);
int z=0;
scanf(“%d”、&z);
如果(z==(x*y)){
printf(“正确响应!\n”);
返回1;
}
printf(“不正确的响应,正确的答案是:%d\n”,x*y);
返回0;
}
int main(){
srand(时间(空));
printf(“将生成并显示两个随机数(1-12)。此游戏的目标是正确猜测数字的乘积。\n”);
int correct=0;
int不正确=0;
int-choice=1;
做{
如果(选项==1){
if(guess()){
++正确的;
}否则{
++不正确;
}
}
printf(“输入1再次播放\n输入2查看统计\n输入3重置统计\n输入4退出:”;
scanf(“%d”,选择(&C);
开关(选择){
案例1:
打破
案例2:
printf(“您已选择查看统计信息\n”);
printf(“正确答案的数量为:%d\n”,正确);
printf(“错误答案的数量为:%d\n”,不正确);
打破
案例3:
printf(“您已选择重置统计信息\n”);
正确=0;
不正确=0;
打破
案例4:
printf(“您已选择退出\n”);
打破
违约:
printf(“无效数字!请输入1到4之间的数字。\n”);
打破
}
}while(选项!=4);
返回0;
}
感谢您的反馈和帮助!!这段代码很有效!一开始创建int guess()函数很聪明(我从未想过要这么做)。非常感谢!将它放入函数中,退出时返回。