gpt4 book ai didi

java - 披萨订购计划

转载 作者:行者123 更新时间:2023-12-01 10:12:59 26 4
gpt4 key购买 nike

public class PizzaEx {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
char letter;
String input;
int sizeD;
int pizzaCount=1;
Pizza pieOne;

do{
sizeD = getValidSize();
input = JOptionPane.showInputDialog(null, "What type of topping do you wish to order? " +
"\n Green Pepper" +
"\n Mushroom"+
"\n Sausage"+
"\n Pepperoni"+
"\n Plain");

pieOne = new Pizza(sizeD, input);
System.out.println(pieOne);
System.out.println("The Number of pizzas made are " + pieOne.getPizzaCount() +"."+"\n");
input = JOptionPane.showInputDialog(null, "Do you wish to continue?\n "+
"'y' or 'Y' for YES\n"+
"'n' or 'N' for NO\n");
letter = input.charAt(0);

pizzaCount = pizzaCount +1;
}
while (letter == 'Y'|| letter == 'y');

System.exit(0);
}

private static int getValidSize()
{
int d;
String input;
do{
input = JOptionPane.showInputDialog(null, "What size of pizza do you wish to order? "+
"\n 9 inch"+
"\n 12 inch"+
"\n 16 inch");
d = Integer.parseInt(input);
} while (!(d==9 || d==12 || d==16));
return d;
}

所以上面是我的主要类(class)

public class Pizza {



private int diameter;
private int numOfPizza;
private double price;
private String tops;

Pizza(int sizeD, String input) {
diameter = sizeD;
tops = input;

}

public int getDiameter(){
return diameter;
}

/**
*
* @param pizzaCount
* @return
*/
public int getPizzaCount(){
return numOfPizza;
}
public double getPrice(){
return price;
}
public String getToppings(){
return tops;
}
public void setDiameter(int sizeD){
if (sizeD == 9)
diameter = 9;
else if ( sizeD == 12)
diameter = 12;
else if (sizeD == 15)
diameter = 15;
else
diameter = 0;
}
public void setPizzaCount(int pizzaCount){
numOfPizza = pizzaCount;
}
public void setPrice(double total){
price = total;
}
public void setToppings(String input){
if ("green pepper".equalsIgnoreCase(input))
tops = "Green Pepper";
else if ("mushroom".equalsIgnoreCase(input))
tops = "Mushroom";
else if ("sausage".equalsIgnoreCase(input))
tops = "Sausage";
else if ("pepperoni".equalsIgnoreCase(input))
tops = "Pepperoni";
else
tops = "Plain";
}
private double calculatePrice(int sizeD, String input){
double total;
if (sizeD == 9 && (tops).equalsIgnoreCase("plain"))
total = 5.95;
else if (sizeD == 9)
total = 6.95;
else if (sizeD == 12 && (tops).equalsIgnoreCase("plain") )
total = 7.95;
else if (sizeD == 12)
total = 8.95;
else if (sizeD == 16 && (tops).equalsIgnoreCase("plain"))
total = 9.95;
else if (sizeD == 16)
total = 10.95;
else
total = 0.0;

return total;
}
public String toString(){
String pizzaString ="You have ordered a "+diameter + " inch pizza with "+tops +" toppings and a price of $"+ calculatePrice(diameter, tops);
return pizzaString;
}

当我打印输出时,即使我设置了 pizzaCount = 1,它仍然显示制作的披萨数量 = 0。另外,当它要求浇头时,如果我输入除有效浇头选择之外的任何字符串 {"green椒", "mushroom", "sausage", "pepperoni", "plain"} 它将计算字符串作为配料,如果配料不是 {"green椒", "mushroom", "sausage", "pepperoni"} 以外的任何东西,则需支付配料费用 “普通”

这不是家庭作业或测试问题。这是我的教授布置的一些额外练习,并不是为了分数。我只是想要一些帮助来澄清为什么 String tops 没有被分配 setToppings() 方法调用的值。

最佳答案

使用getNumOfPizza()总是得到0的原因是因为你从不增加int numOfPizza,你只增加pizzaCount main 中。

至于topping,即使您输入无效字符串,您仍要收取toppings费用的原因是您在calculatePrice中收取​​topping费用的逻辑if !equalsIgnoreCase("plain")。换句话说,除了“plain”之外的任何东西都将被视为配料。事实上,这个方法中的逻辑不必要地复杂,我建议你简化一些if语句:

private double calculatePrice(int sizeD, String input){
if(!(tops).equalsIgnoreCase("plain")) {
total = 1;
} else {
total = 0;
}

if(sizeD == 9) {
total += 5.95;
}

else if(sizeD == 12) {
total += 7.95;
}

else if(sizeD == 16) {
total += 9.95;
}

return total;
}

关于java - 披萨订购计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36045735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com