〈计算提成〉
10万(含)以下 10%提成
10-20万(含)部分7.5%提成
20-40万(含)部分5%
40-60万(含)部分3.5%
60-100万(含)部分1.5%
100万以上1%
public class CounterPercentage {
public static final double level1 = 0.1;
public static final double level2 = 0.075;
public static final double level3 = 0.05;
public static final double level4 = 0.03;
public static final double level5 = 0.015;
public static final double level6 = 0.01;
public static final double bound1 = 100000; //0-100000
public static final double bound2 = 200000; //100000-200000
public static final double bound3 = 400000; //200000-400000
public static final double bound4 = 600000; //400000-600000
public static final double bound5 = 1000000; //600000-100000
public static void main(String[] args) {
System.out.println("请输入净利润:");
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextDouble()){
System.out.println("提成为:"+getPercentage(scanner.nextDouble()));
}
else {
System.out.println("请输入的净利润不合法,请确定后再输入!");
}
}
/**
* 根据输入的净利润计算出提成
* @param netProfits 净利润
* @return 提成
*/
private static double getPercentage(double netProfits ) {
double percentage = 0.0;
if(netProfits <= bound1){
percentage += netProfits*level1;
}
else if(netProfits <= bound2 ){
percentage += getPercentage(bound1) + (netProfits - bound1)*level2;
}
else if(netProfits <= bound3){
percentage += getPercentage(bound2) + ( netProfits - bound2)*level3;
}
else if(netProfits <= bound4){
percentage += getPercentage(bound3) + (netProfits - bound3)*level4;
}
else if(netProfits <= bound5){
percentage += getPercentage(bound4) + (netProfits - bound4)*level5;
}
else if(netProfits > bound5){
percentage += getPercentage(bound5) + (netProfits - bound5)*level6;
}
return percentage;
}
}
测试结果:
image.png