C++ switch 语句计数输出
我在形成该程序的输出语句时遇到问题。正确的输出应该打印与其数值相同的输入数字。 。
I am having trouble forming the output statement for this program. The correct output should print the same input number that its numerical value holds. .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您想要类似的东西:
对于 for 循环,请注意您将从 0 到
<
总数。如果您转到<=
,您就会通过该数字。范围[0,4]
= 5(或者在我们的例子中[0,5)
表示整数集),[1,5] 也是如此
所以请记住注意你的偏移量。祝你好运!编辑:还要认识到
switch
语句的位置已被移动。通过将其放入循环中,您实际上可以在每次运行循环时进行计数。例如,循环在您的cin
行处“阻塞”(换句话说,它等待输入)。当它收到输入时(尽管没有错误检查,因此非 int 值可能会导致段错误),它将根据 switch 语句检查输入并相应地递增。通过将
switch
语句放置在循环之外(正如您最初所拥有的那样),请意识到什么也没有发生。switch
语句中没有default
条件,也没有处理0
的情况。如果您在循环后检查switch
,则standard == 0
因为这是您首先退出循环的方式(因此,standard 的最后一个值将被保留)。此外,重新审视 for 循环语法。这可以通过多种方式完成,这里有很多关于 SO 的好文章,可以通过使用搜索功能进一步帮助您,但它基本上是这样工作的:
STARTING_POINT = 您应该开始循环的值
SOME_CONDITION = 当此条件为真时,循环将终止
SOME_CHANGE = 如何更改变量(即从起点开始)直到达到条件
因此,循环如下所示:
意味着将
int i
初始化为值1
直到
i <= 5
运行完循环然后,在每次迭代中,将
i
加一(即++i
或i++
)最后,如您所见,我添加了一个我的 int 的初始化行。这样做的作用是为您的变量提供一些起始值而不是垃圾值。
我希望这有帮助
问候,
丹尼斯·M.
It looks like you want something similar to this:
For the for loops, note that you are going from 0 to
<
the total number. If you went to<=
you would go passed that number. The range[0,4]
= 5 (or in our case[0,5)
for the set of integers) and so does[1,5]
so remember to mind your offsets. Good luck!EDIT: Also recognize that the position of the
switch
statement has been moved. By placing it in the loop, you actually count every time it runs through the loop. For instance, the loop "blocks" (in other words, it waits for input) at yourcin
line. When it receives an input (though you have no error-checking, so a non-int value may cause a segfault), it will then check it against the switch statement and increment accordingly.By placing the
switch
statement outside the loop (as you initially had it), please realize that nothing happens. You have nodefault
condition in yourswitch
statement, nor a case for handling0
. If you check yourswitch
after the loop,standard == 0
since that is how you exited the loop in the first place (therefore, standard's last value will be retained).Furthermore, revisit the for loop syntax. This can be done in several ways, there are many good articles here on SO which can help you further by using the search function, but it basically works like this:
STARTING_POINT = The value where you should start your loop
SOME_CONDITION = When this condition is true, then the loop will terminate
SOME_CHANGE = how to change your variable (i.e. from starting point) until it reaches the condition
So a loop which looks like this:
means to initialize
int i
to value1
Until
i <= 5
run through your loopThen, on every iteration, increment
i
by one (which is++i
ori++
)Finally, as you can see, I added an initialization line to my int's. What this does is it gives your variables some starting value rather than garbage value.
I hope this helps
Regards,
Dennis M.
您肯定需要在 while 循环内移动
switch
语句(编辑:这是基于 OP 最初发布的代码)。You definitely need to move the
switch
statement within the while loop (Edit: this was based on the OP's initial posting of the code).我真的不明白你想要完成什么,但这部分在这里:
无论输入如何,都会连续打印 6 次“1”。您能多解释一下您想要做什么吗?
I really don't understand what you're trying to accomplish, but this part here:
Is going to print out '1' 6 times in a row, regardless of the input. Could you explain a bit more what you're trying to do?
这是我的解释:您想要输入一个数字序列(以零结尾),然后使用循环输出这些完全相同的数字。
这是一些伪代码来给您提示。
无需为每个可能的数字使用 switch 语句和计数器。只需将每个输入存储在数组中自己的位置即可。
Here's my interpretation: You want to input a sequence of numbers (ending with zero), then output those exact same numbers, using loops.
Here's some pseudocode to give you a hint.
No need to use a switch statement and a counter for each possible number. Just store each input in it's own spot in an array.