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;}所以以上是我的主要课程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 peppers", "mushroom", "sausage", "pepperoni", "plain"}之外的任何字符串,它将字符串视为打顶,并且在应为非{"green peppers", "mushroom", "sausage", "pepperoni"}的任何东西时应收取打顶费这不是家庭作业或测试问题。这是我的教授提出的一些额外练习,并不用于评分。我只需要一些帮助来阐明为什么未为String tops分配方法"plain"所要执行的值。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 之所以总是将0与getNumOfPizza()一起使用,是因为您从不递增int numOfPizza,而仅递增pizzaCount中的main。至于topping,即使您输入了无效的字符串,您仍要收取打顶费的原因是因为您在calculatePrice中的逻辑,如果您在!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; } (adsbygoogle = window.adsbygoogle || []).push({});
10-06 13:37