UT源码 045
(3)设计佣金问题的程序
commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone)、手机壳(Mobile phone shell)、手机贴膜(Cellphone screen protector)三个部件,每个部件单价为:耳机80元,手机壳10元,手机贴膜8元,每月月末向制造商报告销量,制造商根据销量给销售商佣金。如果销售额不足1000元按10%提取佣金,1000-1800元部分按15%提取佣金,超过1800元部分按20%提取佣金。
程序要求:
1)先显示“请分别输入三种手机配件的销售情况:”
2)不满足条件,返回:“输入数量不满足要求”,返回重新输入;
3)条件均满足, 则返回佣金额。返回等待输入。
float commission (int headphone, int shell, int protector)
import java.util.Scanner; public class Main { private static Scanner scanner; /* * 耳机80元,手机壳10元,手机贴膜8元 */ public static float commission(int headphoneNum,int mpShellNum,int csProtectorNum){ int total = headphoneNum*80+mpShellNum*10+csProtectorNum*8; float commission = 0; if(total<1000){ commission = (float) (total*0.1); }else if(total>=1000 && total<=1800){ commission = (float) (1000*0.1+(total-1000)*0.15); }else if(total>1800){ commission = (float) (1000*0.1+800*0.15+(total-1800)*0.2); } return commission; } public static void main(String[] args) { String STYLE = "RUN"; while(STYLE=="RUN"){ System.out.println("请分别输入三种手机配件的销售情况(耳机,手机壳,手机贴膜):"); scanner = new Scanner(System.in); String input = scanner.nextLine(); input = input.replaceAll("\\D", ",").replace("_+", ","); input = input.trim(); String[] str = input.split(","); int[] nums = new int[str.length]; float result = 0; boolean temp = false; String[] part = { "耳机", "手机壳", "手机贴膜" }; if(str.length != 3){ System.out.println("Error:输入数据格式错误,请检查并重新输入。"); continue; } for (int i = 0; i < str.length; i++) { nums[i] = Integer.parseInt(str[i]); while (!temp) { if (nums[i] < 0) { System.out.println("Error:" + part[i] + " 输入数量不满足要求,请重新输入"+ part[i]+ "销售数量。"); @SuppressWarnings("resource") Scanner newNum = new Scanner(System.in); nums[i] = newNum.nextInt(); } temp = true; } temp = false; } result = commission(nums[0], nums[1], nums[2]); System.out.println("总佣金为:" + result); } } }