标签:心得体会 miss input -- string system util amp mission
1.由于测试人员未完成我的测试代码,所以就自我的看法做了一些修改。
代码如下:
import java.util.Scanner; public class CommissionTest { public static final float HEADPHONE_PRICE = 80;// 耳机单价 public static final float PROTCTOR_PRICE = 8;// 手机贴膜单价 public static final float MOBILESHELL_PRICE = 10;// 手机壳单价 public static final float MIN_RATE = 0.1f;// 不足1000的佣金比率 public static final float MID_RATE = 0.15f;// 1000-1800的佣金比率 public static final float MAX_RATE = 0.2f;// 超过1800的佣金比率 public static void main(String args[]) { while (true) { Scanner input = new Scanner(System.in); System.out.println("请分别输入三种手机配件的销售情况"); System.out.println("耳机销售情况:"); int headPhoneNum = checkNum(input.nextLine()); System.out.println("手机壳销售情况:"); int mobPhoneShellNum = checkNum(input.nextLine()); System.out.println("手机贴膜销售情况:"); int cellSrcProNum = checkNum(input.nextLine()); float commission = commission(headPhoneNum, mobPhoneShellNum, cellSrcProNum); System.out.println("收取的佣金为" + commission); System.out.println("是否退出?输入-1退出,按任意数字继续"); if (Integer.parseInt(input.nextLine()) == -1) { System.exit(1); } System.out.println("----------------------------------"); } } public static int checkNum(String num) {// 检查每次输入是否满足条件 Scanner input = new Scanner(System.in); while (true) { if(num.equals("")||num == null){ System.out.println("输入不为空,请重新输入"); num = input.nextLine(); } else if (!num.matches("^[0-9]*$")) { System.out.println("输入数量不满足要求,请重新输入"); num = input.nextLine(); } else { return Integer.parseInt(num); } } } // 计算佣金 public static float commission(int headPhone, int mobPhoneshell, int protctor) { float commission = 0;// 佣金 float headPhoneSale = headPhone * HEADPHONE_PRICE; float mobPhoneShellSale = mobPhoneshell * MOBILESHELL_PRICE; float protctorSale = protctor * PROTCTOR_PRICE; float totalSale = headPhoneSale + mobPhoneShellSale + protctorSale; if (totalSale < 1000 && totalSale > 0) { commission = totalSale * MIN_RATE; } else if (totalSale <= 1800) { commission = totalSale * MID_RATE; } else { commission = totalSale * MAX_RATE; } return commission; } }
2.心得体会:
原先没有做输入为空的控制,这次使用了nextLine进行接收输入数据,并对输入进行了判断。代码命名也更加规范化。
标签:心得体会 miss input -- string system util amp mission
原文地址:http://www.cnblogs.com/lucerner/p/6629373.html