UT源码+159
修改后代码:
设计佣金问题的程序
commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone)、手机壳(Mobile phone shell)、手机贴膜(Cellphone screen protector)三个部件,每个部件单价为:耳机80元,手机壳10元,手机贴膜8元,每月月末向制造商报告销量,制造商根据销量给销售商佣金。如果销售额不足1000元按10%提取佣金,1000-1800元部分按15%提取佣金,超过1800元部分按20%提取佣金。
程序要求:
1)先显示“请分别输入三种手机配件的销售情况:”
2)不满足条件,返回:“输入数量不满足要求”,返回重新输入;
3)条件均满足, 则返回佣金额。返回等待输入。
package demo; import java.io.BufferedReader; import java.io.InputStreamReader; public class Commission { public static final int Pheadphone = 80; public static final int Pshell = 10; public static final int Pprotector = 8; private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); private static int headphone = -1; private static int shell = -1; private static int protector = -1; public static double commission(int headphone,int shell,int protector){ double sum = 0; double salary = 0.0; sum = Pheadphone*headphone + Pshell*shell +Pprotector*protector; if(sum<1000) salary = 0.1*Pheadphone*headphone + 0.1*Pshell*shell +0.1*Pprotector*protector; else if(sum<1800) salary = 0.15*Pheadphone*headphone + 0.15*Pshell*shell +0.15*Pprotector*protector; else salary = 0.2*Pheadphone*headphone + 0.2*Pshell*shell +0.2*Pprotector*protector; return salary; } private static int[] input(){ int a[] = null, i = 0; try { String str = bufferedReader.readLine(); String s[] = str.split("\\s+"); a = new int[s.length]; for(String tmp : s){ a[i++] = Integer.parseInt(tmp); } } catch (Exception e) { a = null; } return a; } public static void main(String[] args) { // TODO 自动生成的方法存根 double salary = 0.0; int exit = 1, a[] = null; while(exit == 1){ System.out.println("请分别输入三种手机配件的销售情况:"); a = input(); if(a == null || a.length != 3){ System.out.println("输入数量不满足要求”,返回重新输入: "); }else{ headphone = a[0]; shell = a[1]; protector = a[2]; } exit = 2; } if(headphone<0||shell<0||protector<0){ System.out.println("输入数量不满足要求"); } salary = commission(headphone,shell,protector); System.out.println("佣金额为:"+salary); headphone = shell = protector = -1; a = null; } }
浙公网安备 33010602011771号