IF和Switch的选择结构运用

题:
 运输公司对用户计算运费.路程(S)越远,每公里运费越低.标准如下:
   s<250km 没有折扣
   250≤S<500 2%折扣
   500≤S<1000 5%折扣
   1000≤S<2000 8%折扣
   2000≤S<3000 10%折扣
   3000≤S 15%折扣
   设每公里每吨货物的基本运费为P(Price的缩写),货物重为w(weight的缩写),距离为S,折扣为d(discount的缩写),


  则总运费f(freight的缩写)的计算公式为f=P*w*S*(1-d)

* if 解题思路*

import java.util.Scanner;
public class Price {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
for(int i = 1; i>0; i++){
double f;
double d=0;
System.out.println("----------------------------------\n程序计算运费,请输入参数!");
System.out.print("请输入每公里每吨基本运费(RMB):");
int p = input.nextInt();
System.out.print("请输入您的货物重量(T):");
int w = input.nextInt();
System.out.print("请输入您运送的总里程(km):");
int s = input.nextInt();
if (s<1){
System.out.println("您的里程过短!");
}else if(s<250){
d=0;
}else if(s<500){
d=2;
}else if(s<1000){
d=5;
}else if(s<2000){
d=8;
}else if(s<3000){
d=1;
}else{
d=15;
}
System.out.printf("您的总运费为:%.2f元\n",f=p*w*s*(1-d/100));
}
}
}

* switch 解题思路*

import java.util.Scanner;
public class Price {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
for(int j = 1; j>0; j++){
double f;
double d=0;
System.out.println("----------------------------------\n程序计算运费,请输入参数!");
System.out.print("请输入每公里每吨基本运费(RMB):");
int p = input.nextInt();
System.out.print("请输入您的货物重量(T):");
int w = input.nextInt();
System.out.print("请输入您运送的总里程(km):");
int s = input.nextInt();
int a =s/250;
switch (a){
case 0:
d=0;
break;
case 1:
d=2;
break;
case 2:
case 3:
d=5;
break;
case 4:
case 5:
case 6:
case 7:
d=8;
break;
case 8:
case 9:
case 10:
case 11:
d=10;
break;
default:
d=15;
}
System.out.printf("您的总运费为:%.2f元\n",f=p*w*s*(1-d/100));
}
}
}

控制台输出-测试

D:\Java\jdk1.8.0_202\bin\java.exe-JAVA_workpace Price
----------------------------------
程序计算运费,请输入参数!
请输入每公里每吨基本运费(RMB):1
请输入您的货物重量(T):1
请输入您运送的总里程(km):1000
您的总运费为:920.00元
----------------------------------
程序计算运费,请输入参数!
请输入每公里每吨基本运费(RMB):1
请输入您的货物重量(T):1
请输入您运送的总里程(km):10000
您的总运费为:8500.00元
----------------------------------
程序计算运费,请输入参数!
请输入每公里每吨基本运费(RMB):
*

*练习结束

*