【发布时间】:2011-09-10 00:59:39
【问题描述】:
首先,我意识到从性能的角度来看,这个 switch 语句的设计很慢,因为在某些情况下会多次调用 cout。除此之外,这种编写 switch 语句的方式不是好的编码习惯吗?换句话说,是单独处理每个案例并打破更好还是失败更好?
int main(void)
{
int number;
cout << "Enter a number between 1 and 10 and I will display its Roman numeral equivalent." << endl
<< "> ";
cin >> number;
cout << "Roman numeral: ";
switch (number)
{
case 3:
cout << "I";
case 2:
cout << "I";
case 1:
cout << "I";
break;
case 4:
cout << "I";
case 5:
cout << "V";
break;
case 6:
cout << "VI";
break;
case 7:
cout << "VII";
break;
case 8:
cout << "VIII";
break;
case 9:
cout << "I";
case 10:
cout << "X";
break;
default:
cout << "Error!\nYou did not enter a number between 1 and 10";
}
cout << endl;
return 0;
}
【问题讨论】:
标签: c++