【发布时间】:2021-11-25 23:51:10
【问题描述】:
我确实尝试过使用这个程序
#include <iostream>
using namespace std;
int main()
{
string x;
string a = "black";
string b = "red";
string c = "white";
int e, f, g = 0; //e = black; f = red; h = white;
cout << "input car color :";
cin >> x;
if (x == a) {
cout << "continue input car color?" << endl;
}
else if (x == b) {
cout << "continue input car color?" << endl;
}
return 0;
}
但我不知道最后一个显示用户输入了多少颜色
这是我的程序的结果,我该如何做?顺便说一句,它在 c++ 中
input car color: black //black,red,white=user input
continue input car color? (y/n)? y
input car color: black
continue input car color? (y/n)? y
input car color: black
continue input car color? (y/n)? y
input car color: red
continue input car color? (y/n)? y
input car color: white
continue input car color? (y/n)? n
detail car color
black 3 car
red 1 car
white 1 car
【问题讨论】:
-
你标记了
loops。你知道循环是如何工作的吗? -
不是用于循环输入汽车颜色的问题> 还是 goto , while, for?
-
不要使用
goto。如果您提前知道要运行多少次迭代,请使用for循环。当您不知道要运行多少次迭代时,请使用while循环。不过,在这种情况下,do..while循环会更合适。 -
goto很难正确。要正确并获得易于阅读和易于维护的代码更难。更难说服其他需要使用您的代码的人goto是正确的选择。我发现使用goto注定你必须不断地使用goto进行防御,而且这比在没有goto的情况下编写代码或重写代码以不使用goto来快速吸收更多的时间。
标签: c++ loops while-loop do-while goto