优化C ++案例陈述 [英] Optimizing C++ Case Statement
问题描述
嘿伙计们,我有这个程序,我需要上课。以下是说明:
编写一个名为foo的函数,询问用户的年龄。将年龄值传递给名为showAge的函数,该函数使用选择案例结构打印出以下内容:如果年龄为0-12,则打印出您仍然是孩子。如果年龄是13 - 19打印出你是一个少年。如果年龄为20-40岁,则您是成年人。什么其他打印你开始变得成熟
这就是我所拥有的,它的作品,但我不明白为什么我的教导不会让我使用if else。 .... 请帮我。这是正确的解决方案吗?
Hey guys i have this program that i need to write for class. here are the instructions:
Write a function called foo that asks the user for their age. Pass the age value to a function called showAge that uses a select case structure to print out the following: If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager. If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature
This is what i have, its works but i dont see why my teach wont let me use an if else..... please help me. Is this the right solution?
推荐答案
哇,这是一个地狱案例陈述。我建议。对于if-elses来说,这是一个很好的解决方法。
:: Edit ::您也可以使用if语句来调用函数。如果if语句设置正确,则一个语句不会与另一个语句重叠,您可以省略else语句。然后你没有使用if-elses ...
Wow, that is one hell of a case statement. I would recommend the ternary operator, though. It''s a nifty workaround to if-elses.
::: You could also use an if statement to call a function. If you if statements were set up properly, one would not overlap with the other, and you could leave out the else statement. Then you''re not using if-elses...
我想我会这样做:
I GUESS I WILL DO THIS:
我想我会这样做:
I GUESS I WILL DO THIS:
这也有效。您是否看过三元运算符?
That works too. Did you look at the ternary operator?
这篇关于优化C ++案例陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!