程序可以运行,但是您需要根据单词输入大写或小写字母。同样,当您选择2个或多个标准/豪华浇头时,它仅显示每个浇头的最后一个输入。绝对等待帮助:D。谢谢!
public class Sundae {
private final double SALES_TAX = .08625;
private final double DELUXE_TOPPING = 1.25;
private String iceCreamFlavor;
private int numberOfScoops;
private double costForScoops;
private String standardToppingList;
private String freeSyrupChoice;
private String deluxeToppingList;
private double costOfDeluxeToppings;
private int counterD;
private double costOfSundae;
private double tax;
private double total;
public Sundae()
{
}
public String getIceCreamFlavor()
{
return iceCreamFlavor;
}
public int getNumberOfScoops()
{
return numberOfScoops;
}
public double getCostForScoops()
{
return costForScoops;
}
public String getStandardToppingList()
{
return standardToppingList;
}
public String getFreeSyrupChoice()
{
return freeSyrupChoice;
}
public String getDeluxeToppingList()
{
return deluxeToppingList;
}
public double getCostOfDeluxeToppings()
{
return costOfDeluxeToppings;
}
public int getCounterD()
{
return counterD;
}
public double getCostOfSundae()
{
return costOfSundae;
}
public void setIceCreamFlavor(String choice)
{
iceCreamFlavor = choice;
}
public void setNumberOfScoops(int numberScoops)
{
numberOfScoops = numberScoops;
}
public void setCostForScoops()
{
costForScoops = numberOfScoops + 1.79;
}
public void setCounterD(int numberD)
{
counterD = numberD;
}
public void setStandardToppingList(String standardTopping)
{
standardToppingList = standardTopping;
}
public void setFreeSyrupChoice(String syrup)
{
freeSyrupChoice = syrup;
}
public void setDeluxeToppingList(String deluxeTopping)
{
deluxeToppingList = deluxeTopping;
}
public void setDefault()
{
iceCreamFlavor = "Vanilla";
numberOfScoops = 2;
costForScoops = 1.79;
standardToppingList = "Whipped Cream, Hot Fudge, Multi-Colored Sprinkles, and a cherry.";
}
public void Print()
{
costForScoops = numberOfScoops * 1.79;
costOfDeluxeToppings = DELUXE_TOPPING * counterD;
costOfSundae = costForScoops + costOfDeluxeToppings;
tax = SALES_TAX * costOfSundae;
total = costOfSundae + tax;
System.out.println("Flavor: " + iceCreamFlavor + "\nNumber of scoops: " + numberOfScoops + "\nCost for scoops: " + costForScoops
+ "\nStandard toppings: " + standardToppingList + "\nSyrup:" + freeSyrupChoice + "\nDeluxe toppings: " + deluxeToppingList
+ "\nCost of deluxe toppings: " + costOfDeluxeToppings);
System.out.printf("Subtotal: $%.2f \nSale Tax: $%.2f \nTotal: $%.2f", costOfSundae, tax, total);
}
}
驱动程序
import java.util.Scanner;
public class SundaeDriver {
public static void main(String[]args) {
Sundae sundae = new Sundae();
int numberS;
int numberD;
Scanner input = new Scanner(System.in);
System.out.println("Which sundae flavor do you want? ");
sundae.setIceCreamFlavor(input.nextLine());
if ((sundae.getIceCreamFlavor().equalsIgnoreCase("Vanilla"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("French Vanilla"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Peanut Butter"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Chocolate"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Chocolate Chip"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Chocolate Chip Cookie"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Cookie Dough"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Ice Cream Cake"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("American Dream"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Vanilla Chocolate Swirl"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Strawberry"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Mint Chocolate Chip"))||
(sundae.getIceCreamFlavor().equalsIgnoreCase("Oreo Cookies and Cream")))
{
}
else
{
sundae.setIceCreamFlavor("Vanilla");
}
System.out.println("How many scoops do you want?");
sundae.setNumberOfScoops(input.nextInt());
if ((sundae.getNumberOfScoops() == 1)||
(sundae.getNumberOfScoops() == 2)||
(sundae.getNumberOfScoops() == 3)||
(sundae.getNumberOfScoops() == 4)||
(sundae.getNumberOfScoops() == 5)||
(sundae.getNumberOfScoops() == 6))
{
}
else
{
sundae.setNumberOfScoops(2);
}
System.out.println("How many free toppings do you want?(free)");
numberS = input.nextInt();
input.nextLine();
for (int i = 0; i < numberS;i++)
{
System.out.println((i + 1) + " free toppings do you want?");
sundae.setStandardToppingList(input.nextLine());
if ((sundae.getStandardToppingList().equals("Whipped cream"))||
(sundae.getStandardToppingList().equals("Syrup"))||
(sundae.getStandardToppingList().equals("Multi colored sprinkles"))||
(sundae.getStandardToppingList().equals("Cherry")))
{
sundae.getStandardToppingList();
}
else
{
sundae.setStandardToppingList("Whipped cream, hot fudge, multi colored sprinkles and cherry");
}
}
System.out.println("Which syrup do you want?");
sundae.setFreeSyrupChoice(input.nextLine());
if ((sundae.getFreeSyrupChoice().equals("Hot fudge"))||
(sundae.getFreeSyrupChoice().equals("Chocolate"))||
(sundae.getFreeSyrupChoice().equals("Caramel")))
{
}
else
{
sundae.setFreeSyrupChoice("");
}
System.out.println("How many deluxe toppings do you want?");
numberD = input.nextInt();
sundae.setCounterD(numberD);
sundae.getCounterD();
input.nextLine();
for(int j = 0; j < numberD; j++)
{
System.out.println((j + 1) + " deluxe toppings do you want?");
sundae.setDeluxeToppingList(input.nextLine());
if((sundae.getDeluxeToppingList().equals("M&Ms"))||
(sundae.getDeluxeToppingList().equals("Crushed oreos"))||
(sundae.getDeluxeToppingList().equals("Reeses")||
(sundae.getDeluxeToppingList().equals("Chocolate chips"))||
(sundae.getDeluxeToppingList().equals("KitKats"))||
(sundae.getDeluxeToppingList().equals("Gummy Bears"))||
(sundae.getDeluxeToppingList().equals("Cookie Dough Bits"))||
(sundae.getDeluxeToppingList().equals("Marshmallows"))||
(sundae.getDeluxeToppingList().equals("Peanuts"))||
(sundae.getDeluxeToppingList().equals("Walnuts"))))
{
sundae.getDeluxeToppingList();
}
else
{
sundae.setDeluxeToppingList("");
}
}
sundae.Print();
input.close();
}
}
答案 0 :(得分:0)
这似乎是使用enum
的绝佳时机。我将为第一种情况提供一个示例。冰淇淋口味。
public enum IceCreamFlavors {
VANILLA, FRENCH_VANILLA, PEANUT_BUTTER, CHOCOLATE, CHOCOLATE_CHIP,
CHOCOLATE_CHIP_COOKIE, COOKIE_DOUGH, ICE_CREAM_CAKE, AMERICAN_DREAM,
VANILLA_CHOCOLATE_SWIRL, STRAWBERRY, MINT_CHOCOLATE_CHIP, OREOS_COOKIES_AND_CREAM;
public static IceCreamFlavors fromName(String name) {
for (IceCreamFlavors f : values()) {
if (f.name().replace('_', ' ').equalsIgnoreCase(name)) {
return f;
}
}
return IceCreamFlavors.VANILLA;
}
}
然后您可以这样称呼
System.out.println("Which ice cream flavor do you want? ");
IceCreamFlavors flavor = IceCreamFlavors.fromName(input.nextLine());
这样,您可以验证枚举中保留的风味的逻辑。
答案 1 :(得分:0)
使用:
private List<String> standardToppingList;
private List<String> deluxToppingList;
构造函数包含:
standardToppingList = new Arraylist<>();
deluxToppingList = new Arraylist<>();
要添加到列表中,请使用:
standardToppingList.add(standardTopping);
deluxToppingList.add(deluxTopping);
并使用以下显示方式:
for(String s : deluxToppingList) {
System.out.println(s);
}
for(String s : standardToppingList) {
System.out.println(s);
}