程序应该输出可变长度的垂直和水平线使用星号。 星号和方向的数量将会是决定由用户输入的语句。 它必须使用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 + +。 谢谢!