-1

其目的是询问用户是否想玩、输出选项、接收用户输入,然后模拟“滚动”适当面的骰子。输出结果,然后要求另一个选择,等等。

程序的第一个循环正常工作,但它没有询问您是否要再次播放,而是空白。如果您输入一个数字来选择骰子,它将输出第一个骰子有多少面,无论选择什么。

#include <iostream>     
#include <ctime>        
using namespace std;


int throwDie(int Sides, int &throwResult)
{
throwResult = 1 + rand() % (Sides - 1 + 1); 
return throwResult;                      
}
int main()
{           
int dieTot = 0,
        throwNumber = 0,
    numberSides = 0,
    throwResult = 0;
    int die1 = 4;
    int die2 = 6;
    int die3 = 8;
    int die4 = 10;
    int die5 = 12;
    int die6 = 20;

    char rollAgain;
    srand(unsigned(time(0) ));
    START:   
    cout << "Do you want Play? ";
    cin >> rollAgain;

    cout << "How many sides? " << endl;
    cout << "1 - 4 sided die\n";
    cout << "2 - 6 sided die\n";
    cout << "3 - 8 sided die\n";
    cout << "4 - 10 sided die\n";
    cout << "5 - 12 sided die\n";
    cout << "6 - 20 sided die\n";

    int choice;
    cout << "Enter choice: ";
    cin >> choice;

    switch( choice ) {
        case 1:
            do {
                cout << "You have choosen a 4 sided die? " << endl;
                int numberSides = die1;
                cout << numberSides << "-sided die rolled for a value of "
                     << throwDie(numberSides, throwResult) << "!" << endl;
                dieTot = dieTot + throwResult;
                throwNumber++;
                cin >> numberSides;
            } while (choice != numberSides );
            goto START;
            break;

        case 2:
            do {
                cout << "You have choosen a 6 sided die? " << endl;
                int numberSides = die2;
                cout << numberSides << "-sided die rolled for a value of "
                     << throwDie(numberSides, throwResult) << "!" << endl;
                dieTot = dieTot + throwResult;
                throwNumber++;
                cin >> numberSides;
                cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;
            } while (choice != numberSides );
            break;

        case 3:
            do {
                cout << "You have choosen a 8 sided die? " << endl;
                int numberSides = die3;
                cout << numberSides << "-sided die rolled for a value of "
                     << throwDie(numberSides, throwResult) << "!" << endl;
                dieTot = dieTot + throwResult;
                throwNumber++;
                cin >> numberSides;
                cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;
            } while (choice != numberSides );
            break;

        case 4:
            do {
                cout << "You have choosen a 10 sided die? " << endl;
                int numberSides = die4;
                cout << numberSides << "-sided die rolled for a value of "
                     << throwDie(numberSides, throwResult) << "!" << endl;
                dieTot = dieTot + throwResult;
                throwNumber++;
                cin >> numberSides;
                cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;
            } while (choice != numberSides );
            break;

        case 5:
            do {
                cout << "You have choosen a 12 sided die? " << endl;
                int numberSides = die5;
                cout << numberSides << "-sided die rolled for a value of "
                     << throwDie(numberSides, throwResult) << "!" << endl;
                dieTot = dieTot + throwResult;
                throwNumber++;
                cin >> numberSides;
                cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;
            } while (choice != numberSides );
            break;

        case 6:
            do {
                cout << "You have choosen a 20 sided die? " << endl;
                int numberSides = die6;
                cout << numberSides << "-sided die rolled for a value of "
                     << throwDie(numberSides, throwResult) << "!" << endl;
                dieTot = dieTot + throwResult;
                throwNumber++;
                cin >> numberSides;
                cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;
            } while (choice != numberSides );
            break;

        default:
            cout << "Not a proper entry.\n";
            break;

        cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;

        return 0;
    }
}

我假设每个案例陈述中的额外代码是:

    cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides,                              throwResult) << "!" << endl;
    dieTot = dieTot + throwResult;
    throwNumber++;
    cin >> numberSides;
    cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;
    }
while (choice != numberSides );
    break;

那会在代码的末尾吗?并非在每个案例陈述中。另外,如果我这样做,我什至需要将 while/do 放入函数中吗?

4

1 回答 1

1

这与循环无关。

  1. 尝试打印出从您的 cin 中获得的值,您会发现问题所在。
  2. 您不一致地使用 goto 语句,仅START在第一种情况下返回
  3. 您没有正确终止您的 switch 语句
  4. 你应该永远不要使用 goto ,除非你不能以任何其他方式做到这一点。
  5. 这写的太可怕了。为什么需要在每个 case 语句中复制所有内容?似乎您只需要使用开关来设置 numberSides 的值和打印输出.. 其余的应该很常见...我认为您也不需要变量...请参阅我的原始代码重构简单的事情...

重构代码使其更具可读性:见下文

#include <iostream>     
#include <ctime>        
using namespace std;

//This function will throw a dice returning the result while updating the parameter.
int throwDie(int Sides, int &throwResult)
{
    throwResult = 1 + rand() % (Sides); //Sides - 1 + 1 == Sides
    return throwResult;                      
}

//Display the menu
void displayMenu()
{
    cout << "How many sides? \n"
         << "1 - 4 sided die\n"
         << "2 - 6 sided die\n"
         << "3 - 8 sided die\n"
         << "4 - 10 sided die\n"
         << "5 - 12 sided die\n"
         << "6 - 20 sided die\n"
         << "CTRL-D to quit";
}


int main()
{           
    int dieTot = 0,
        throwNumber = 0,
        numberSides = 0,
        throwResult = 0;
    // THESE ARENT NEEDED
    // sides = 4+2*(X-1)
    //int die1 = 4;
    //int die2 = 6;
    //int die3 = 8;
    //int die4 = 10;
    //int die5 = 12;
    //int die6 = 20;

    srand(unsigned(time(0) ));

    displayMenu();

    int choice;

    while ( cin >> choice ) {
        /* cin >> choice will return 0 / fail if they enter CTRL-D or enter
           something other than a number... you'll have to handle that in another way */

        if ( choice < 1 || choice > 6 ) {
            cout << "Invalid choice try again!";
            continue;
        }

        //numberSides = 4+2*(choice-1); //fast  way

        switch (choice) {
            case 6:
                numberSides = 20;
                break;
            case 1:
                //if you dont want to do this slick trick of fall through
                //numberSides = 4;
                //break; 
            case 2:
            case 3:
            case 4:
            case 5:
                numberSides = 4+2*(choice-1); // use this for 1,2,3,4,5;
                break;
        }

        cout << "You have chosen a " << numberSides << " sided die.\n"
             << numberSides << "-sided die rolled for a value of "
             << throwDie(numberSides, throwResult) << "!" << endl;
        dieTot += throwResult;
        throwNumber++;
    }

    cout << "\n\n" << "Total for " << throwNumber << " throws = " << dieTot << endl;
    return 0;
} 
于 2013-09-11T23:22:32.863 回答