第二次作业+105032014063

测试人员的测试帖链接:http://www.cnblogs.com/ChenXh123/p/6593024.html

第一次测试结果:

  被测试代码在语句覆盖、逻辑覆盖测试通过。

测试人员代码优化建议:

  1)if、else、else if等语句自占一行,无论执行语句有多少(即使只有一行执行语句),都要加{};

  2)代码中要注意数据类型的转换,以免丢失数据。

    如:ans=(sum-1800.0)*0.2+220.0;  //从“double”转换到“float”,可能丢失数据

  3)依据C\C++编程规范,多元运算符和他们的操作符之间至少需要一个空格,这个也需要注意。

 

代码:

import java.util.Scanner;

public class Commission{
	//String To Int
	public static int changeToInt(String number){
		int ans=0;
		try{
			ans = Integer.parseInt(number);
		} 
		catch (Exception e){
			ans=-1;
			// TODO: handle exception
		}
		return ans;
	}
	
	//计算佣金
	public static String commission(int headphone, int shell, int protector){
		if(headphone < 0 || shell < 0 || protector < 0){
			return "输入格式错误,请重试\n";
		}
		
	    double ans = 0.0;
	    long sum = 80 * headphone + 10 * shell + 8 * protector;
	    
	    if(sum < 1000){
	    	ans = sum * 0.1;
	    }
	    else if(sum >= 1000 && sum <= 1800){
	    	ans = 100.0 + (sum - 1000) * 0.15;
	    }
	    else{
	    	ans = (sum - 1800.0) * 0.2 + 220.0;
	    }
	    
	    return String.format("佣金金额:%.2f元\n", ans);
	}
	
	public static void main(String[] args){
		while(true){
			String headphone;	//耳机
			String shell;		//手机壳
			String protector;	//手机贴膜
			
			@SuppressWarnings("resource")
			Scanner scanner = new Scanner(System.in);
			
			System.out.println("请输入耳机的销售情况:");
			headphone = scanner.next();
	        System.out.println("请输入手机壳的销售情况:");
	        shell = scanner.next();
	        System.out.println("请输入手机贴膜的销售情况:");
	        protector = scanner.next();
	        
	        System.out.println(commission(changeToInt(headphone), changeToInt(shell), changeToInt(protector)));
		}
	}
}

心得体会:

还是C好。

posted @ 2017-03-24 18:56  ChainYugi  阅读(330)  评论(2编辑  收藏  举报