减乘除\); operators=sc.nextInt();
double sum=CalculatorMethod(a,b);
System.out.println(a+\和\+b+\运算的结果是:\+sum);
sc.close(); }
private static double CalculatorMethod(int a, int b) { double sum=0;
switch(operators){ case 1:sum=a+b; break;
case 2:sum=a-b; break;
case 3:sum=a*b; break;
case 4:sum=a/b; }
return sum; }
}
13. (if 语句)*托运计费问题:
当货物重量小于 20 公斤的时候,收费 5 元,大于 20 公斤小于 100 公斤的时 候超出 20 公斤的部分按每 0.2 元每公斤计费,如果超出 100 公斤的时候,超出 的部分按照每公斤 0.15 元计算。
读入货物的重量,输出计算之后货物的运费。 答:public class CheckFee {
public static void main(String[] args) { double w=130; double price=0; if(w<=20){ price=5;
}else if(w>20&&w<=100){ price=5+(w-20)*0.2; }else if(w>100){
price=5+(100-20)*0.2+(w-100)*0.15; }
System.out.println(price); }
}
6