C++ switch 语句计数输出

发布于 2024-10-31 14:46:34 字数 48 浏览 2 评论 0原文

我在形成该程序的输出语句时遇到问题。正确的输出应该打印与其数值相同的输入数字。 。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

街道布景 2024-11-07 14:46:34

看起来您想要类似的东西:

#include <iostream>

using namespace std;

int main()
{
    int standard, count1, count2, count3, count4, count5;

    // Initialize your int's
    standard = count1 = count2 = count3 = count4 = count5 = 0;

    do {
        cout<< "Enter value: ";
        cin >> standard;

        // Switch through your cases
        switch(standard)
        {
        case 1:
            count1++;
        break;
        case 2:
            count2++;
        break;
        case 3:
            count3++;
        break;
        case 4:
            count4++;
        break;
        case 5:
            count5++;
        break;
        }
    } while(standard != 0);

    // Do this type of loop for all your count vars
    for(int i=0;i<count1;++i)
        cout<< "1";

    for(int i=0;i<count2;++i)
        cout<< "2";

    // And so on...

    return 0;
}

对于 for 循环,请注意您将从 0 到 < 总数。如果您转到<=,您就会通过该数字。范围 [0,4] = 5(或者在我们的例子中 [0,5) 表示整数集),[1,5] 也是如此 所以请记住注意你的偏移量。祝你好运!

编辑:还要认识到switch语句的位置已被移动。通过将其放入循环中,您实际上可以在每次运行循环时进行计数。例如,循环在您的 cin 行处“阻塞”(换句话说,它等待输入)。当它收到输入时(尽管没有错误检查,因此非 int 值可能会导致段错误),它将根据 switch 语句检查输入并相应地递增。

通过将 switch 语句放置在循环之外(正如您最初所拥有的那样),请意识到什么也没有发生。 switch 语句中没有 default 条件,也没有处理 0 的情况。如果您在循环后检查 switch,则 standard == 0 因为这是您首先退出循环的方式(因此,standard 的最后一个值将被保留)。

此外,重新审视 for 循环语法。这可以通过多种方式完成,这里有很多关于 SO 的好文章,可以通过使用搜索功能进一步帮助您,但它基本上是这样工作的:

for(STARTING_POINT;SOME_CONDITION;SOME_CHANGE);

STARTING_POINT = 您应该开始循环的值
SOME_CONDITION = 当此条件为真时,循环将终止
SOME_CHANGE = 如何更改变量(即从起点开始)直到达到条件

因此,循环如下所示:

for(int i=1;i<=5;++i){ ... }

意味着将 int i 初始化为值 1
直到 i <= 5 运行完循环
然后,在每次迭代中,将 i 加一(即 ++ii++

最后,如您所见,我添加了一个我的 int 的初始化行。这样做的作用是为您的变量提供一些起始值而不是垃圾值。

我希望这有帮助

问候,
丹尼斯·M.

It looks like you want something similar to this:

#include <iostream>

using namespace std;

int main()
{
    int standard, count1, count2, count3, count4, count5;

    // Initialize your int's
    standard = count1 = count2 = count3 = count4 = count5 = 0;

    do {
        cout<< "Enter value: ";
        cin >> standard;

        // Switch through your cases
        switch(standard)
        {
        case 1:
            count1++;
        break;
        case 2:
            count2++;
        break;
        case 3:
            count3++;
        break;
        case 4:
            count4++;
        break;
        case 5:
            count5++;
        break;
        }
    } while(standard != 0);

    // Do this type of loop for all your count vars
    for(int i=0;i<count1;++i)
        cout<< "1";

    for(int i=0;i<count2;++i)
        cout<< "2";

    // And so on...

    return 0;
}

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 your cin 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 no default condition in your switch statement, nor a case for handling 0. If you check your switch 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:

for(STARTING_POINT;SOME_CONDITION;SOME_CHANGE);

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:

for(int i=1;i<=5;++i){ ... }

means to initialize int i to value 1
Until i <= 5 run through your loop
Then, on every iteration, increment i by one (which is ++i or i++)

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.

灯角 2024-11-07 14:46:34

您肯定需要在 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).

琉璃繁缕 2024-11-07 14:46:34

我真的不明白你想要完成什么,但这部分在这里:

  for (count1=0; count1<=5; count1++)
        cout << "1";

无论输入如何,都会连续打印 6 次“1”。您能多解释一下您想要做什么吗?

I really don't understand what you're trying to accomplish, but this part here:

  for (count1=0; count1<=5; count1++)
        cout << "1";

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?

甜扑 2024-11-07 14:46:34

这是我的解释:您想要输入一个数字序列(以零结尾),然后使用循环输出这些完全相同的数字。

这是一些伪代码来给您提示。

Declare an array of int's large enough to hold all your input numbers.
Declare a counter c = 0.

Begin loop while input is not 0
    Input number from user and store it in array[c].
    Increment counter c.
End loop

Begin loop i from 0 to c-1
    Output number that is in array[i].
End loop

无需为每个可能的数字使用 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.

Declare an array of int's large enough to hold all your input numbers.
Declare a counter c = 0.

Begin loop while input is not 0
    Input number from user and store it in array[c].
    Increment counter c.
End loop

Begin loop i from 0 to c-1
    Output number that is in array[i].
End loop

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文