我的交换机出现问题,在一个值完成操作后,我没有选择重做菜单,但是当我选择b作为我的菜单值时,我可以选择重做菜单再玩一次。 我无法找到原因,因为测试是在开关声明之外完成的。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main (){
int seed = time(0);
srand(seed);
int num1, randomnum1, toothpicks=21, count1=1, rounds=0, p1picks;
char choice1, choice;
do{ //game menu
cout << "Welcome!\n";
cout << "Please select the game you will be playing.\n";
cout << "Choose 'a' for the RANDOM NUMBER GAME \n";
cout << "Choose 'b' for the TOOTHPICK GAME \n";
cin >> choice1;
cout << "Thank you!\n";
//game menu
switch(choice1){
case 'a': //random number game
//generate random number
randomnum1 = rand() % 100 + 1;
//intro
cout << "Welcome!\n ";
cout << "In this game, you will select a random number between 1 and 100 and see if you can guess it correctly!\n";
cout << "Please type a random number between 1 and 100. \n";
cin >> num1;
//user number validation
while(num1<1 || num1>100){
cout << "Wrong input! \n";
cout << "Input a number between 1 and 100. \n";
cin >> num1;
}
//was user correct?
while(num1!=randomnum1){
if (num1>randomnum1){
cout <<"That's not it. Try guessing a lower number.\n";
cin >> num1;
}
else if (num1<randomnum1){
cout << "Thats not it. Try guessing a higher number.\n";
cin >> num1;
}
count1++;
}
cout << "YES! Congradultions! That's the correct number!\n";
// tell them how many times it took to finish
cout << "Thank you for playing!\n";
return 0;
break;
case 'b': //toothpick game
toothpicks=21;
cout << "Welcome to the Toothpick game. \n";//intro and rules
cout << "Rules: \n";
cout << "Take turns picking the number of toothpicks. There are a total of 21. \n";
cout << "When it is your turn you choose to pick up 1, 2 or three toothpicks. \n";
cout << "The goal is to not be the player that has the last toothpick \n";
cout << "Good luck! \n\n\n";
cout << "You will go first. \n";
while(rounds<5){ //setting rounds to five
cout << "Type the number of toothpicks you will 'pick up'. 1, 2 or 3? \n";
cin >> p1picks;
while(p1picks<1 || p1picks>3){ //validation
cout << "That is not a valid input. You must type 1, 2 or 3. \n";
cin >> p1picks;
}
//menu options for user input
switch(p1picks){
case 1:
cout << "The computer choses to pick up 3 toothpicks. \n";
break;
case 2:
cout << "The computer choses to pick up 2 toothpicks. \n";
break;
case 3:
cout << "The computer choses to pick up 1 toothpicks. \n";
break;
}
rounds++;
toothpicks=toothpicks-4;
cout<<"This is the amount of toothpicks you have left: "<< toothpicks<< endl;
}
//computer winning speech
cout << "The computer has won. Sucker! \n";
break;
default:
cout << "Sorry this is not a choice! /n";
break;
}
cout << "this is inside the switch";
cout << "Would you like to continue? (Y)es or (N)? " << endl;
cin >> choice;
}while (choice == 'Y' || choice == 'y');
cout << "Thank you for playing! /n";
cout << "Goodbye!\n";
return 0;
}
答案 0 :(得分:3)
因为您从a
开关案例末尾的函数返回:
cout << "Thank you for playing!\n";
return 0;
答案 1 :(得分:2)
我的交换机出现问题,在某个值完成操作后,我没有选择重做菜单,
那是因为你有
return 0;
在那个分支中。
如果简化代码,将更容易避免此类错误。而不是在case
语句下放置大量代码,创建辅助函数并调用函数。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int get_choice()
{
int choice;
//game menu
cout << "Welcome!\n";
cout << "Please select the game you will be playing.\n";
cout << "Choose 'a' for the RANDOM NUMBER GAME \n";
cout << "Choose 'b' for the TOOTHPICK GAME \n";
cin >> choice;
cout << "Thank you!\n";
return choice;
}
void playRandomNumberGame()
{
int randomnum1;
int num1;
int count1 = 1;
//generate random number
randomnum1 = rand() % 100 + 1;
//intro
cout << "Welcome!\n ";
cout << "In this game, you will select a random number between 1 and 100 and see if you can guess it correctly!\n";
cout << "Please type a random number between 1 and 100. \n";
cin >> num1;
//user number validation
while(num1<1 || num1>100)
{
cout << "Wrong input! \n";
cout << "Input a number between 1 and 100. \n";
cin >> num1;
}
//was user correct?
while(num1!=randomnum1)
{
if (num1>randomnum1)
{
cout <<"That's not it. Try guessing a lower number.\n";
cin >> num1;
}
else if (num1<randomnum1)
{
cout << "Thats not it. Try guessing a higher number.\n";
cin >> num1;
}
count1++;
}
cout << "YES! Congradultions! That's the correct number!\n";
// tell them how many times it took to finish
cout << "Thank you for playing!\n";
}
void playToothpickGame()
{
int toothpicks=21;
int p1picks;
int rounds = 0;
//intro and rules
cout << "Welcome to the Toothpick game. \n";
cout << "Rules: \n";
cout << "Take turns picking the number of toothpicks. There are a total of 21. \n";
cout << "When it is your turn you choose to pick up 1, 2 or three toothpicks. \n";
cout << "The goal is to not be the player that has the last toothpick \n";
cout << "Good luck! \n\n\n";
cout << "You will go first. \n";
while(rounds<5) //setting rounds to five
{
cout << "Type the number of toothpicks you will 'pick up'. 1, 2 or 3? \n";
cin >> p1picks;
//validation
while(p1picks<1 || p1picks>3)
{
cout << "That is not a valid input. You must type 1, 2 or 3. \n";
cin >> p1picks;
}
//menu options for user input
switch(p1picks)
{
case 1:
cout << "The computer choses to pick up 3 toothpicks. \n";
break;
case 2:
cout << "The computer choses to pick up 2 toothpicks. \n";
break;
case 3:
cout << "The computer choses to pick up 1 toothpicks. \n";
break;
}
rounds++;
toothpicks=toothpicks-4;
cout<<"This is the amount of toothpicks you have left: "<< toothpicks<< endl;
}
//computer winning speech
cout << "The computer has won. Sucker! \n";
}
int main ()
{
int seed = time(0);
srand(seed);
char choice1, choice;
do
{
choice1 = get_choice();
//game menu
switch(choice1)
{
case 'a': //random number game
playRandomNumberGame();
break;
case 'b': //toothpick game
playToothpickGame();
break;
default:
cout << "Sorry this is not a choice! /n";
break;
}
cout << "this is inside the switch";
cout << "Would you like to continue? (Y)es or (N)? " << endl;
cin >> choice;
}
while (choice == 'Y' || choice == 'y');
cout << "Thank you for playing! /n";
cout << "Goodbye!\n";
return 0;
}