【问题标题】:Why won't my switch statements correctly output asterisks in my C++ program?为什么我的 switch 语句不能在我的 C++ 程序中正确输出星号?
【发布时间】:2019-03-24 00:49:47
【问题描述】:

该程序应该使用星号输出不同长度的垂直和水平线。星号的数量和方向由用户在输入语句中决定。它必须使用 switch 语句创建。这是我当前的代码:

int main() {

    // Variables
    int length = 1;
    char direct;

    // User input choice
    if (length >= 1 && length <= 20) {
        cout << "\nEnter the line length and direction: ";
        cin >> length >> direct;
    }

    // If user input incorrect
    else {
        system("pause");
    }

    // Switch cases for horizontal or vertical
    switch (direct) {
    case 'h': for (int count = 0; count <= length; count++) {
        cout << "*";
        break;
    }
    case 'H': for (int count = 0; count <= length; count++) {
        cout << "*";
        break;
    }
    case 'V': for (int count = 0; count <= length; count++) {
        cout << "*" << "\n" << endl;
        break;
    }
    case 'v': for (int count = 0; count <= length; count++) {
        cout << "*" << "\n" << endl;
        break;
    }

    default:  cout << "Illegal comand" << endl;

    }

    system("pasue");
}

这是我的水平选择输出语句之一的样子:

Enter the line length and direction: 4 h
***

*

Illegal Command

这是我的一个垂直选择输出语句的样子:

Enter the line length and direction: 4 v

*

Illegal Command

这是我希望水平的样子:

Enter the line length and direction: 4 h

****

这是我希望垂直的样子:

Enter the line length and direction: 4 v

*
*
*
*

为什么星号不能正确输出?为什么每次都输出“Illegal Command”?还认为我应该注意我是 C++ 的初学者。谢谢!

【问题讨论】:

标签: c++ for-loop switch-statement break


【解决方案1】:

尝试在每个 switch 条件中将 break 关键字放在大括号之外。

这样做

case 'V': for (int count = 0; count <= length; count++) {
        cout << "*" << "\n" << endl;

    }
break;

而不是

case 'V': for (int count = 0; count <= length; count++) {
            cout << "*" << "\n" << endl;
            break;
        }

【讨论】:

    【解决方案2】:

    您将 break; 语句放在错误的位置。

    case 'h': for (int count = 0; count <= length; count++) {
        cout << "*";
        break;
    }
    

    需要;

    case 'h': for (int count = 0; count <= length; count++) {
        cout << "*";
      }
      break;
    

    请注意,hH 的逻辑相同。您可以将它们组合起来。

    case 'h':
    case 'H':
      for (int count = 0; count <= length; count++) {
        cout << "*";
      }
      break;
    

    您可以类似地组合vV 的情况。

    您仍然可以通过创建辅助函数来编写水平线和垂直线来改进它。

    case 'h':
    case 'H':
      writeHorizontalLine(length);
      break;
    
    case 'v':
    case 'V':
      writeVerticalLine(length);
      break;
    

    在哪里

    void writeHorizontalLine(int length)
    {
       for (int count = 0; count <= length; count++)
       {
          cout << "*";
       }
       cout << endl;
    }
    
    void writeHorizontalLine(int length)
    {
       for (int count = 0; count <= length; count++)
       {
          cout << "*" << endl;
       }
    }
    

    【讨论】:

    • 非常感谢!所有这些都帮助了很多:)
    【解决方案3】:

    在for循环外写break语句。如果你在for循环内写break语句以防万一它退出for循环。遇到非法命令是因为你没有在for循环外使用break

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 2020-02-28
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 2014-05-21
      • 2021-04-16
      相关资源
      最近更新 更多