关于switch case语句的问题

2025-02-27 22:07:04
推荐回答(5个)
回答1:

#include 
using namespace std;
int main()
{
    double Hour,PayPer,Pay;
    do
    {
        cout<<"输入工作时数及单位时间基本工资:";
        cin>>Hour>>PayPer;
        bool f1=(Hour>40);
        bool f2=(Hour>50);
        switch(f1)
        {
            case 0:
            Pay=Hour*PayPer;
            cout<<"输出:"<            break;
            case 1:
            switch(f2)
            {
            case 0:
                    Pay=40*PayPer+(Hour-40)*1.5*PayPer;
                    cout<<"输出:"<                    break;
            case 1:
                    Pay=40*PayPer+10*1.5*PayPer+(Hour-50)*3*PayPer;
                    cout<<"输出:"<                    break;
            }
            break;
        }
    }while(Hour!=0&&PayPer!=0);
    return 0;
}
参考我的这个代码,题目在
http://zhidao.baidu.com/question/494793277752591964.html?oldq=1
满意记得采纳哦!

回答2:

错误是:每个case
语句必须包含一个常量表达式做其变量,这个值不能在switch
语句的各级中出现
多次
意思是switch()
{
case(a);
case(b);
}
a和b的值不能相等,15||-3||12结果为真(即为1)-15||3||-12结果为真(即为1),两个1重复了,所以报错。建议改为
switch
(b)
{
case
(15):
{cout<<"
The
winner
is
player1!";
break;}
case
(-3):
{cout<<"
The
winner
is
player1!";
break;}
case
(12):
{cout<<"
The
winner
is
player1!";
break;}
case
(-15):
{cout<<"The
winner
is
player2!";
break;}
case
(3):
{cout<<"The
winner
is
player2!";
break;}
case
(-12):
{cout<<"The
winner
is
player2!";
break;}
default:
cout<<"Both
of
you
are
winners!";
}

回答3:

确实不行。编译器报错(case label does not reduce to an integer constant)

如果硬要用switch-case,只能另外设定一个变量b, 当a在[1000,2000]区间里面的时候赋值b=1;
if(a<=2000 && a>=1000)b=1;

switch(b)
case 1: p=p*0.9;

case 2: ............;

.........
.........

回答4:

int n;
float price;
if (1000这种方式来设置

回答5:

case 1000 ... 2000 : 解决