【发布时间】: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++ 的初学者。谢谢!
【问题讨论】:
-
请尝试learn how to debug your code,如果您这样做问题应该很明显。提示:
break语句在哪里?
标签: c++ for-loop switch-statement break