程序设计思想:
1.根据折扣情况建立一个计算折扣的方法
(1)1本无折扣
(2)两本折扣5%
(3)三本折扣10%
(4)四本折扣20%
(5)五本折扣25%
2.主方法定义num,count,remain 3个整形,price1个double
3.让用户输入买书的本数num,在count = num/10,price=price+num*10*0.75
4.remian = num%10,判断remain是否大于5
5。大于,remain2 = remain%5,price=price+50*0.75
6.将remain2放入计算折扣的方法中,price=price+result
7.小于,直接将remain放入计算折扣的方法中,返回的值result,价格price=result+price
8.输出最优结果
package 课上作业_价格; import java.util.Scanner; public class test { public static double calculate(int num){ double result=0; if(num==1){ result=num*10+result; } else if(num==2){ result=num*10*0.95+result; } else if(num == 3){ result=num*3*0.9+result; } else if(num == 4){ result=num+4*0.8+result; } else if(num == 5){ result = num*5*0.75+result; } return result; } public static void main(String args[]){ int num, remain, count; double price=0; Scanner sc = new Scanner(System.in); System.out.println("请输入图书的本数:"); num = sc.nextInt(); count = num/10; price = count*100*0.75+price; remain = num%10; if(remain<=5){ double result; result = calculate(remain); price =price + result; } else{ int remain2; remain2=remain%5; price = price+50*0.75; price = price+calculate(remain2); } System.out.println("最优价格是:"+price); } }
截图结果: