繁体   English   中英

C ++它不会切换到case(2)

[英]c++ it does not switch to case(2)

#include<iostream>
using namespace std;
void main()
{
    int c=0, sum = 0, y;
    cout << "press (1) to find the sum of 2 even numbers between 2 integers or press (2) to find all the prime numbers between 10 and 30,or press -1 to terminate";
    cin >> c;
    while (c != -1)
    {
        switch (c)
        {
        case (1) :
        {int x=0;
            cout << "enter 2 integers\n";
            cin >> x >> y;
            while (x<= y)
            {
                if (x % 2 == 0)
                    sum = sum +x;
                x++;
            }
            cout << "sum is" << sum;
            break;
        }
        case(2) :
        {int counter = 30, counter2 = 1, nod = 0;
            while (counter >= 10)
            {
                while (counter2 >= counter)
                {
                    if (counter%counter2 == 0)
                        nod = nod + 1;
                    counter2++;
                }
                if (nod == 2)
                    cout << counter << " is prime number";
                counter--;
            }
            break; }
        default: cout << "wrong input";
        }
    }
}

如果我按1,情况1可以正常工作,默认情况下也可以正常使用。 哨兵循环也很好。 但是,当我按2时,它将切换为默认值。 怎么了? 我是C ++的新手,但是这个问题从未发生过。

您的代码工作正常(除非main必须返回int ):

现场例子

可能只是您的案例2没有按照您的想法去做。 由于这一行:

while (counter2 >= counter)

并且counter2 == 1counter == 30的事实将永远不会进入,而case (2)将什么也不打印。

您有一个永无止境的循环,请检查以下内容:

#include<iostream>
using namespace std;
int main()
{
    int c=0, sum = 0, y;
    cout << "press (1) to find the sum of 2 even numbers between 2 integers or press (2) to find all the prime numbers between 10 and 30,or press -1 to terminate";
    cin >> c;
    while (c != 1)
    {
        switch (c)
        {
        case (1) :
        {int x=0;
            cout << "enter 2 integers\n";
            cin >> x >> y;
            while (x<= y)
            {
                if (x % 2 == 0)
                    sum = sum +x;
                x++;
            }
            cout << "sum is" << sum;
            break;
        }
        case(2) :
        {int counter = 30, counter2 = 1, nod = 0;
            while (counter >= 10)
            {
                cout << "hello im case 2";
                while (counter2 >= counter)
                {
                    if (counter%counter2 == 0)
                        nod = nod + 1;
                    counter2++;
                }
                if (nod == 2)
                    cout << counter << " is prime number";
                counter--;
            }
           } break; 
        default: cout << "wrong input";
        }
    }
}

在这里检查: http : //cpp.sh/5ond

do {
  cin >> c;
  switch {
    case (1): {
      // Your code
    }
    case (2): {
      // Your code
    }
    case (-1): {
      // Terminate
    }
    default: {
      // Your code
    }
  }
} while (true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM