Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 开关语句_C++_Switch Statement - Fatal编程技术网

C++ 开关语句

C++ 开关语句,c++,switch-statement,C++,Switch Statement,使用switch语句创建一个代码,当除以8时输出余数(0、1、2、3和其他)。用户输入从0到99的20个整数。每个余数应显示其总数 这就是输出的方式 Total number with remainder 0 is 4. Total number with remainder 1 is 6. Total number with remainder 2 is 5. Total number with remainder 3 is 3. Total number of other remainde

使用switch语句创建一个代码,当除以8时输出余数(0、1、2、3和其他)。用户输入从0到99的20个整数。每个余数应显示其总数

这就是输出的方式

Total number with remainder 0 is 4. 
Total number with remainder 1 is 6.
Total number with remainder 2 is 5.
Total number with remainder 3 is 3.
Total number of other remainder is 2.
/

#包括
使用名称空间std;
int main()
{
int i,x[20];

coutSwitch语句应该有一个测试条件,然后根据结果执行命令。这里的问题是,您正在测试一个没有值的变量。括号中包含正在测试的数据。这些情况是根据结果值执行给定的命令。您在这里测试的是
x[i]%8
。这应该在括号中的中间。大小写应该只附加值

switch (x[i] % 8) {
    case 0: //...
    case 1: //...
    case 2: //...
    case 3: //...
    default: //...
}

case
将测试括号中的操作结果是否等于其指定值(例如0、1、2、3或默认值),并执行指定的命令。

开关(变量){case-somevalue;case-othervalue
case
应该是值,而不是表达式。它们不是一个
if
语句。所以在我的例子中,它将是case-o,case-1等等,我假设…尝试
switch(x[i]%8){case-0:…case-1:…case-7:…}
。你还需要一个
来循环switch语句。
switch (x[i] % 8) {
    case 0: //...
    case 1: //...
    case 2: //...
    case 3: //...
    default: //...
}