首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将if etc语句转换为开关

将if etc语句转换为开关
EN

Stack Overflow用户
提问于 2019-05-28 05:49:57
回答 1查看 0关注 0票数 0

在将if-etc语句转换为开关时,我遇到了问题。我正在创建一个在C+中的Tic-Tac-脚趾游戏。我不确定如何在开关本身内使用2D数组矩阵。本练习的目的是单独切换。下面是我到目前为止是如何编写代码的代码。

代码语言:txt
复制
switch (a)
{
case 1:
    if(matrix[0][0] == '1')
    matrix[0][0] = player_1;

    else
    {
        cout << "Field is already in use. Try again" << endl;
        input(); //Calling function input to prompt user to key in value again and again
    }
    break;

case 2:
    if(matrix[0][1] == '2')
    matrix[0][1] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input(); //Calling function input to prompt user to key in value again and again
    }
    break;
case 3:
    if(matrix[0][2] == '3')
    matrix[0][2] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input(); //Calling function input to prompt user to key in value again and again
    }
    break;
case 4:
    if(matrix[1][0] == '4')
    matrix[1][0] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input(); //Calling function input to prompt user to key in value again and again
    }
    break;
case 5:
    if(matrix[1][1] == '5')
    matrix[1][1] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
    break;
case 6:
    if(matrix[1][2] == '6')
    matrix[1][2] = player_1;
    else {

        cout << "Field is already in use. Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
    break;
case 7:
    if(matrix[2][0] == '7')
    matrix[2][0] = player_1;
    else {

        cout << "Field is already in use.Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
    break;
case 8:
    if(matrix[2][1] == '8')
    matrix[2][1] = player_1; 
    else {

        cout << "Field is already in use. Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
    break;
case 9:
    if(matrix[2][2] == '9')
    matrix[2][2] = player_1;

    else {

        cout << "Field is already in use. Try again" << endl;
        input();//Calling function input to prompt user to key in value again and again
    }
        break;
default:
    cout << "Number inputted doesn't exist. Please try again" << endl;
    input(); //Calling function input to prompt user to key in value again and again
}
EN

回答 1

Stack Overflow用户

发布于 2019-05-28 15:34:26

如果必须使用switch,可以通过以下方式计算位置(行和列)来简化代码switch

代码语言:javascript
复制
int row = 0;
int column = 0;
bool position_is_valid = true;
switch (a)
{
    case 1:  row = 0; column = 0; break;
    case 2:  row = 0; column = 1; break;
    case 3:  row = 0; column = 2; break;
    case 4:  row = 1; column = 0; break;
    case 5:  row = 1; column = 1; break;
    case 6:  row = 1; column = 2; break;
    case 7:  row = 2; column = 0; break;
    case 8:  row = 2; column = 1; break;
    case 9:  row = 2; column = 2; break;
    default: position_is_valid = false; break;
}
if (position_is_valid)
{
    if (matrix[row][column]) == '0' + a)
    {
        matrix[row][column] = player1;
    }
    else
    {
        std::cout << "Position is not empty.\n";
    }
}
else
{
    std::cout << "Position is invalid.\n";
}

通常,如果您重复代码或查看模式,则可以简化代码或简化设计。

编辑1:更多代码switch 您可以将空框字符的计算添加到开关中:

代码语言:javascript
复制
    char empty_field_value = ' ';
    switch (a)
    {
        case 1:  row = 0; column = 0; empty_field_value = '1'; break;
        case 2:  row = 0; column = 1; empty_field_value = '2'; break;
        case 3:  row = 0; column = 2; empty_field_value = '3'; break;
        case 4:  row = 1; column = 0; empty_field_value = '4'; break;
        case 5:  row = 1; column = 1; empty_field_value = '5'; break;
        case 6:  row = 1; column = 2; empty_field_value = '6'; break;
        case 7:  row = 2; column = 0; empty_field_value = '7'; break;
        case 8:  row = 2; column = 1; empty_field_value = '8'; break;
        case 9:  row = 2; column = 2; empty_field_value = '9'; break;
        default: position_is_valid = false; break;
    }
    if (position_is_valid)
    {
        if (matrix[row][column]) == empty_field_value)
        {
            matrix[row][column] = player1;
        }
        else
        {
            std::cout << "Position is not empty.\n";
        }
    }
    else
    {
        std::cout << "Position is invalid.\n";
    }

我建议不要将if语句放入switch,因为它们不依赖于switch值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档