第二次作业+105032014159
1.测试帖链接
http://www.cnblogs.com/ddanwu-blogs/p/6593698.html
2.提出的问题、发现的缺陷
(1)可以适当的给代码添加一些注释,增加可读性;
(2)没有循环;
(3)输入设置没有控制好;
(4)建议不要用数组来接收输入的值,应该给每次的输入进行判断,省去不必要的输入,增加程序的灵活性;
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;//销售额小于1000时所抽取的佣金
else if(sum<1800)
salary = 0.15*Pheadphone*headphone + 0.15*Pshell*shell +0.15*Pprotector*protector;//计算销售额1000到1800的佣金
else
salary = 0.2*Pheadphone*headphone + 0.2*Pshell*shell +0.2*Pprotector*protector;//销售额1800以上的佣金
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;
}
}
4.修正后心得体会:
添加了必要的注释,增加了代码的可读性。在以后的代码编译过程里要对代码的注释多加书写使代码更通俗易懂。读题目的过程中需要细心,对题目的理解要深入,根据需求来设计,以测试的角度来寻找自己的缺陷。
浙公网安备 33010602011771号