第二次作業+105032014138
被測代碼地址:http://www.cnblogs.com/lyz007/p/6530327.html
2.提出的問題、發現的缺陷
1)在測試字母數字混合輸入的情況下不能很好的進行合法性判斷。
2)if與else嵌套語句太多,代碼讀起來較復雜。數量邊界沒有設定清楚
3)代碼建議必要的註釋
3.修改後的代碼
import java.util.Scanner;
public class Caculator {
public static float commission(int Headphone,int Shell,int Protector){
int All = Headphone*80+Shell*10+Protector*8;
float commission = 0;
if(All<1000&&All>=0)
{
commission = (float) (All*0.1);
System.out.println("本次銷售所得傭金為:"+commission);
}
else if(All>=1000 && All<=1800)
{
commission = (float) (100+(All-1000)*0.15);
System.out.println("本次銷售所得傭金為:"+commission);
}
else if(All>1800)
{
commission = (float) (100+800*0.15+(All-1800)*0.2);
System.out.println("本次銷售所得傭金為:"+commission);
}
return commission;
}
public static void main(String[] args){
while(true)
{
int Headphone=0;
int Shell=0;
int Protector=0;
Scanner scanner=new Scanner(System.in) ;
try{
System.out.println("請輸入耳機的銷售情況:");
Headphone=scanner.nextInt();
System.out.println("請輸入手機殼的銷售情況:");
Shell=scanner.nextInt();
System.out.println("請輸入手機膜的銷售情況:");
Protector=scanner.nextInt();
scanner.close();
}catch(Exception e){
System.out.println("出現異常:"+e);
System.exit(-1);
}
if(Headphone>=0&&Shell>=0&&Protector>=0)
{
commission(Headphone,Shell,Protector);
}
else
{
System.out.println("輸入有誤,請重新輸入!");
}
}
}
}
學習心得
(1)我明白了在編寫代碼時思路是很重要的,要多加思考才能發現更加簡潔的設計方案
(2)我發現很多錯誤都來源與粗心大意,明白了以後在編寫代碼時要仔細檢查,例如在計算當銷售金額大於1800時所得到的擁金,如果好好檢查就能避免這樣的錯誤
(3)通過對本章知識的學習,進一步了解了軟件測試,同時通過對本章的學習也使我在編寫代碼時更加嚴謹了
第二次作業+105032014138