首页 > 代码库 > 第二次作业+105032014033
第二次作业+105032014033
1.测试代码连接:http://www.cnblogs.com/5257yu/p/6601603.html
2.提出的建议:
1)该代码没有按照要求将结果作为字符串输出。
2)使用 if 时,应当确保判断条件内所有的范围都能涉及到, if 后面应当接else或者else if。
3.修改后代码:
import java.util.Scanner; public class Yongjin01 { public double Input(double number){ double num = 0; Scanner scanner = new Scanner(System.in); try{ if(num<0){ System.out.println("输入的数量不满足要求!"); }else{ num = scanner.nextDouble(); return num; } }catch(Exception e){System.out.println("输入不合法!");} return num; } public double Commission(double headphone,double shell,double protector){ double commission = 0; double headphonePrice = 80; double shellPrice = 10; double protectorPrice = 8; double headphonesales = headphonePrice*headphone; double shellsales = shellPrice*shell; double protectorsales = protectorPrice*protector; double sales = headphonesales+shellsales+protectorsales; if(sales>1800){ commission = 0.10*1000; commission = commission+0.15*800; commission = commission+0.20*(sales-1800); }else if(sales>1000){ commission = 0.10*1000; commission = commission+0.15*(sales-1000); } else commission = 0.10*sales; return commission; } }
public class ruance2 { public static void main(String[] args) { while(true){ Yongjin01 yj = new Yongjin01(); System.out.println("请分别输入三种手机配件的销售情况:"); double headphone = yj.Input(0); double shell = yj.Input(0); double protector = yj.Input(0); System.out.println("耳机数量:" + headphone + "\n手机壳数量:" + shell + " \n手机贴膜数量:" + protector); double commission = yj.Commission(headphone, shell, protector); System.out.println("销售佣金:" + commission); } } }
4.心得体会
根据测试者的建议增加了else if判定,解决了输入数较小是出现负数的问题,改进了程序的功能,能够符合题意,符合实际。
第二次作业+105032014033
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。