为什么我的switch语句在C ++程序中不能正确输出星号?

克里斯·A

该程序应该使用星号输出不同长度的垂直和水平线。星号的数量和行进方向由用户在输入语句中确定。必须使用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

*
*
*
*

为什么星号无法正确输出?为什么每次都会输出“非法命令”?还以为我应该注意,我是C ++的初学者。谢谢!

萨胡

您将break;陈述放在错误的位置。

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

需要是;

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

请注意,您h和和具有相同的逻辑H您可以将它们结合在一起。

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;
   }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我的程序忽略了switch语句?

为什么我不能在类中放置 switch 语句

为什么我的if语句不能在两个嵌套执行中确定正确的输出?

为什么我不能使用 switch 语句来缩小 Typescript 中的类类型?

为什么switch语句不提供输出?

为什么我的程序不能正确接受另一个程序的管道输出?

为什么我们不能在 switch 语句中使用关系表达式?

为什么当我输入正确的输入时switch语句被忽略

为什么我不能将其他类的常量添加到 java 中的 switch 语句中?

为什么我的 v-for 语句不能正确响应 Vue?

为什么在Java 7中switch语句比String语句要快?

为什么在C / C ++中交织switch / for / if语句是有效的?

为什么我的 If 语句不能正常工作?

为什么我的 if 语句不能正常工作?

为什么 switch 语句在这个 C 计算器中显示默认值?

为什么我的editText不能在android中显示正确的输出?

为什么我的程序不断跳过 if 语句?

为什么我不能在C ++中的三元条件语句中使用“ break”语句?

Switch语句:为什么在不同情况下我不能使用相同的变量名?爪哇

为什么不能在switch语句中声明变量?

为什么在switch语句中不能完全限定枚举值?

为什么在switch语句中最终的Byte不能编译?

为什么在Java的switch语句中不能使用“ continue”?

为什么不能在字符串上使用switch语句?

为什么我的 else/if 语句在 javascript 中不能正常工作?

为什么我的代码中的 if 语句被忽略?

为什么这段代码不能正确输出?

如何使我的if语句输出正确的printf C程序

是否给出“为什么不能将switch语句应用于字符串”的答案?即使使用C ++ 11/14还是正确的吗?