C++ 我能';我不能让我的开关功能工作

C++ 我能';我不能让我的开关功能工作,c++,switch-statement,C++,Switch Statement,问题是将1到10的所有值相加,不包括值3和6,但我不知道为什么我的开关函数不过滤掉3和6。 这是我的密码 #include <iostream> using namespace std; int main() { int a=1,total=0; while (a<=10) { a++; switch (a) { case 3: continu

问题是将1到10的所有值相加,不包括值3和6,但我不知道为什么我的开关函数不过滤掉3和6。 这是我的密码

#include <iostream>
using namespace std;

int main() 
{
    int a=1,total=0;

    while (a<=10)
    {
        a++;

        switch (a)
        {
            case 3:
                continue;
            case 6:
                continue;
            default:
                total = total + a;
        }
    }

    cout << "Sum of the total numbers are " << total << endl;

    return 0;
}
#包括
使用名称空间std;
int main()
{
INTA=1,总计=0;

while(a如果将以下内容添加到while循环的末尾:

cout << "Current total: " << total << " a=" << a << endl;
正如你所看到的,它正确地跳过了3和6,但是它缺少了1,增加了11,这两件事我想你没有预料到

另外,您使用的是
continue
。对于switch语句,您希望使用
break
来阻止在当前案例之后执行案例。(为了详细说明这一点,我认为
continue
可以,因为我认为它正在做您想要的事情:将控制权转移回while语句。但是,如果
a++
在switch语句之后移动,这将不起作用。如果在
0
处启动
a
,请将条件更改为
a<10continue
语句而不是
break

如果您将
a++;
移动到while循环的末尾并修复
continue
语句,我相信它将如您所期望的那样工作

担心我的编辑可能会混淆这一点,这里有两种方法可以帮助您构建代码以获得所需的结果:

#include <iostream>
using namespace std;

int main() 
{
    int a=1,total=0;
    while (a<=10)
    {
        switch (a)
        {
            case 3:
                break;
            case 6:
                break;
            default:
                total = total + a;
        }
        a++;
    }
    cout << "Sum of the total numbers are " << total << endl;
    return 0;
}
#包括
使用名称空间std;
int main()
{
INTA=1,总计=0;

while(a如果将以下内容添加到while循环的末尾:

cout << "Current total: " << total << " a=" << a << endl;
正如你所看到的,它正确地跳过了3和6,但是它缺少了1,增加了11,这两件事我想你没有预料到

另外,您使用的是
continue
。对于switch语句,您希望使用
break
来阻止在当前案例之后执行案例。(为了详细说明这一点,我认为
continue
可以,因为我认为它正在做您想要的事情:将控制权转移回while语句。但是,如果
a++
在switch语句之后移动,这将不起作用。如果在
0
处启动
a
,请将条件更改为
a<10continue
语句而不是
break

如果您将
a++;
移动到while循环的末尾并修复
continue
语句,我相信它将如您所期望的那样工作

担心我的编辑可能会混淆这一点,这里有两种方法可以帮助您构建代码以获得所需的结果:

#include <iostream>
using namespace std;

int main() 
{
    int a=1,total=0;
    while (a<=10)
    {
        switch (a)
        {
            case 3:
                break;
            case 6:
                break;
            default:
                total = total + a;
        }
        a++;
    }
    cout << "Sum of the total numbers are " << total << endl;
    return 0;
}
#包括
使用名称空间std;
int main()
{
INTA=1,总计=0;

虽然(a您的代码工作正常,但您在中央循环中只会犯一些小错误:a从2变为11,而不是从1变为10。它应该是这样的:

Current total: 2 a=2
Current total: 6 a=4
Current total: 11 a=5
Current total: 18 a=7
Current total: 26 a=8
Current total: 35 a=9
Current total: 45 a=10
Current total: 56 a=11
Sum of the total numbers are 56
int a=0, total=0;

while (a < 10)
{
    a++;

    // rest of code
}
int a=0,total=0;

while (a < 10)
{
    a++;
    switch (a)
    {
        case 3:
            break;
        case 6:
            break;
        default:
            total = total + a;
    }
    // in every case you will get here; even if a==3 or a==6
}
EDIT2 如果您希望让循环从1到10,也可以这样做:

int a=1,total=0;

while (a <= 10)
{
    switch (a)
    {
        case 3:
            break;
        case 6:
            break;
        default:
            total = total + a;
    }
    // in every case you will get here; even if a==3 or a==6
    a++;
}
inta=1,总计=0;

虽然(a您的代码工作正常,但您在中央循环中只会犯一些小错误:a从2变为11,而不是从1变为10。它应该是这样的:

Current total: 2 a=2
Current total: 6 a=4
Current total: 11 a=5
Current total: 18 a=7
Current total: 26 a=8
Current total: 35 a=9
Current total: 45 a=10
Current total: 56 a=11
Sum of the total numbers are 56
int a=0, total=0;

while (a < 10)
{
    a++;

    // rest of code
}
int a=0,total=0;

while (a < 10)
{
    a++;
    switch (a)
    {
        case 3:
            break;
        case 6:
            break;
        default:
            total = total + a;
    }
    // in every case you will get here; even if a==3 or a==6
}
EDIT2 如果您希望让循环从1到10,也可以这样做:

int a=1,total=0;

while (a <= 10)
{
    switch (a)
    {
        case 3:
            break;
        case 6:
            break;
        default:
            total = total + a;
    }
    // in every case you will get here; even if a==3 or a==6
    a++;
}
inta=1,总计=0;

while(嗯,应该是。顺便说一句,为什么不使用for循环呢?这样会使它不那么容易出错:您正在对2..11进行求和!另外,从开关内部继续不是很惯用的方法,中断(退出开关)会更常见。我的输出是56。这是2到11之间的数字之和,不包括3和6。我想你只是被a实际上从11开始到2这一事实弄糊涂了。当你得到你不期望的答案时,如果你想寻求帮助,不要假设答案为什么是错的!答案是如您所愿,3和6被跳过,但正如@PeterSchneider所述,您的值列表是2…11,而不是1…10。在默认子句中,添加
cout@EdHeal我认为,这里的安全点可能不是要获得<代码> 1…10代码>的总和,而是要学习如何在C++中使用一些构造(在这种情况下,一般是循环)。.Hmm..it应该。顺便说一句,为什么不使用for循环呢?这将减少出错的可能性:您正在对2..11进行求和!另外,从交换机内部继续不是很习惯的做法,一个中断(退出交换机)会更常见。我的输出是56。这是2到11之间的数字之和,不包括3和6。我想你只是被a实际上从11开始到2这一事实弄糊涂了。当你得到你不期望的答案时,如果你想寻求帮助,不要假设答案为什么是错的!答案是如您所愿,3和6被跳过,但正如@PeterSchneider所述,您的值列表是2…11,而不是1…10。在默认子句中,添加
cout@EdHeal我认为,这里的安全点可能不是要获得<代码> 1…10代码>的总和,而是要学习如何在C++中使用一些构造(在这种情况下,一般是循环)。.更好(第一个括号),现在你得到+1:-)如果它是a@user3499156确实是这样;当a=9时,它将最后一次进入该循环。循环中的第一个命令是:“a++”;因此a将变成10…@ChrisMaes ahhh,被我自己的代码搞笑了。我会注意下一次我将增量放在哪里。@user3499156从0到9在c/c++代码中非常常见。但是,如果您喜欢从1循环到10,这是可能的…我会在我的答案中添加这一点…如果您对其中一个答案感到满意,您可以接受answ呃…更好(第一个括号),如果是a@user3499156确实是这样;当a=9时,它将最后一次进入该循环。循环中的第一个命令是:“a++”。因此,a将变成10…@ChrisMaes ahhh,这是我自己的代码恶作剧。我会注意我将增量ne放在哪里