首页 > 代码库 > 第二次作业+105032014138

第二次作业+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