请写出与以下else-if语句等价的switch语句。
if(choice == 1) price = 3.0;
else if(choice == 2) price = 2.5;
else if(choice == 3) price = 4.0;
else if(choice == 4) price = 3.5;
else{ price = 0.0; }
switch
{
case 1: price=3.0; break;
case 2: price=2.5; break;
case 3:price=4.0;break;
case 4: price=3.5;break;
price=0.0; break;
}
if(choice == 1) price = 3.0;
else if(choice == 2) price = 2.5;
else if(choice == 3) price = 4.0;
else if(choice == 4) price = 3.5;
else{ price = 0.0; }
switch
{
case 1: price=3.0; break;
case 2: price=2.5; break;
case 3:price=4.0;break;
case 4: price=3.5;break;
price=0.0; break;
}
举一反三
- 设变量已正确定义,选项()与以下程序段不等价。switch(choice){case1:price=3.0;case2:price=2.5;default:price=0.0;}A.price=0.0;B.switch(choice){default:price=0.0;case2:price=2.5;case1:price=3.0;}C.price=0.0;switch(choice){case1:price=3.0;case2:price=2.5;}D.if(choice==1){price=3.0;price=2.5;price=0.0;}elseif(choice==2){price=2.5;price=0.0;}else{price=0.0;}
- 以下代码的输出结果是()。publicclassTest{publicstaticvoidmain(String[]args){double[]price=newdouble[5];price[0]=98.10;price[1]=32.18;price[2]=77.75;System.out.print((int)price[0]+""+(int)price[1]+""+(int)price[2]+""+(int)price[3]+""+(int)price[4]);}}
- 以下选项中与if(a==1) a=b;else a++;语句功能不同的switch语句是( )。(a == 1 中的符号是恒等于) A: switch(a==1) {case 0: a=b;break; case 1:a++;} B: switch(a) {case 1: a=b;break; default:a++;} C: switch(a) {default:a++;break; case 1: a=b;} D: switch(a==1) {case 1: a=b;break; case 0:a++;}
- 中国大学MOOC: When the price of a good increases, all else equal, the higher price
- 与分支语句有关的关键词有if、else、switch、case和break