基础类 /*4种主食4种调料,每次只能选1种主食和n种调料,直接输出总价格*/class BuffetFood {private String FoodName;private int FoodPrice;public BuffetFood(String foodName, int foodPrice) {super();FoodName = foodNam
/*4种主食4种调料,每次只能选1种主食和n种调料,直接输出总价格*/ class BuffetFood { private String FoodName; private int FoodPrice; public BuffetFood(String foodName, int foodPrice) { super(); FoodName = foodName; FoodPrice = foodPrice; } public BuffetFood() { super(); // TODO Auto-generated constructor stub } public String getFoodName() { return FoodName; } public void setFoodName(String foodName) { FoodName = foodName; } public int getFoodPrice() { return FoodPrice; } public void setFoodPrice(int foodPrice) { FoodPrice = foodPrice; } @Override public String toString() { return "主食=" + FoodName + ", 价格=" + FoodPrice; } }计算类
/*4种主食4种调料,每次只能选1种主食和n种调料,直接输出总价格*/ import java.util.Scanner; /* * 现在有4种调料,每种都有自己的价格 * 现在有4种主食,每种都有自己的价格 * 任意挑选1种主食和n种调料,然后算出价格 * 输入主食的代码,输入调料的代码, * 输出总价格 * */ public class Buffet { final static BuffetFood q=new BuffetFood("全麦面包",10); final static BuffetFood f=new BuffetFood("蜂蜜燕麦包",11); final static BuffetFood n=new BuffetFood("奶酪面包",12); final static BuffetFood b=new BuffetFood("白面包",13); final static BuffetFood d=new BuffetFood("蛋黄酱",5); final static BuffetFood h=new BuffetFood("黄芥末酱",4); final static BuffetFood s=new BuffetFood("烧烤酱",3); final static BuffetFood F=new BuffetFood("番茄酱",5); static int price = 0; static Scanner sc=new Scanner(System.in); static int count =4; public static void main(String[] args){ System.out.println("请输入你要的主食的数字(1全麦面包;2蜂蜜燕麦包;3奶酪面包;4白面包)ps:本店店大欺客,只准选一种主食"); int x=sc.nextInt(); switch(x){ case 1: price=price+q.getFoodPrice();break; case 2: price=price+f.getFoodPrice();break; case 3: price=price+n.getFoodPrice();break; case 4: price=price+b.getFoodPrice();break; default: System.out.println("输错了,请确认输入1-4其中一个数字"); } System.out.println("调味料有1蛋黄酱;2黄芥末酱;3烧烤酱;4番茄酱。"+"请输入你要多少种调味料(0到4种)"); int seasonNO =sc.nextInt(); Seasoning(seasonNO); System.out.println(price); } //用递归计算出调味料的价钱 public static int Seasoning(int num){ if(num<0||num>count){ System.out.println("输入有误,请输入0-4"); return price; } if(num==0){ return price; } else{ System.out.println("请输入你要的酱料编号:1蛋黄酱;2黄芥末酱;3烧烤酱;4番茄酱"); int snum =sc.nextInt(); switch(snum){ case 1: price=price+d.getFoodPrice();break; case 2: price=price+h.getFoodPrice();break; case 3: price=price+s.getFoodPrice();break; case 4: price=price+F.getFoodPrice();break; default: System.out.println("输错了,请确认输入1-4其中一个数字"); } if(snum>4){ return 0; }else{ return price+Seasoning(num-1); } } } }