使用java中的Pizza / Pizza订单程序,我不断得到13个错误:class,interface或enum expected。我知道问题应该是大括号,但我还没有找到它。我希望不同的一组(或几组)眼睛能够发现错误。
public Pizza (String size, int cheese, int pepperoni, int ham) {
this.size = size;
if (cheese < 0) {
this.cheese = 0;
}
else {
this.cheese = cheese;
}
if (pepperoni < 0) {
this.pepperoni = 0;
}
else {
this.pepperoni = pepperoni;
}
if (ham < 0) {
this.ham = 0;
}
else {
this.ham = ham;
}
}
public double calcCost() {
if (size.equals("small")) {
return 10 + 2 * (ham + cheese + pepperoni);
}
else if (size.equals("medium")) {
return 12 + 2 * (ham + cheese + pepperoni);
}
else if (size.equals("large")) {
return 14 + 2 * (ham + cheese + pepperoni);
}
else {
System.out.println("Invalid size");
}
}
public class PizzaOrder {
private Pizza pizza1;
private Pizza pizza2;
private Pizza pizza3;
public PizzaOrder() {
numPizzas = 1;
pizza1 = new Pizza();
pizza2 = new Pizza();
pizza3 = null;
}
public PizzaOrder (int numPizzas, Pizza pizza1, Pizza pizza2, Pizza pizza3) {
this.numPizzas = numPizzas;
this.pizza1 = pizza1;
this.pizza2 = pizza2;
this.pizza3 = pizza3;
}
public void setNumPizzas (int numPizzas) {
if (numPizzas > 3) {
this.numPizzas = 3;
}
else if (numPizzas < 1) {
this.numPizzas = 0;
}
else {
this.numPizzas = NumPizzas;
}
}
public void setPizza1 (Pizza pizza1) {
this.pizza1 = pizza1;
}
public void setPizza2 (Pizza pizza2) {
this.pizza2 = pizza2;
}
public void setPizza3 (Pizza pizza3) {
this.pizza3 = pizza3;
}
public double calcTotal() {
double total = pizza1.calcCost();
if (numPizzas >= 2) {
total += pizza2.calcCost();
}
if (numPizzas == 3) {
total += pizza3.calcCost();
}
return total;
}
}
public class MainMethodClassName {
public static void main (String args[]) {
Pizza pizza1 = new Pizza("large", 1, 0, 1);
Pizza pizza2 = new Pizza("medium", 2, 2, 0);
PizzaOrder order = new PizzaOrder();
order.setNumPizzas(2);
order.setPizza1(pizza1);
order.setPizza2(pizza2);
double total = order.calcTotal;
System.out.println("First Pizza: ");
System.out.println(pizza1.getDescription());
System.out.println("Second Pizza: ");
System.out.println(pizza2.getDescription());
System.out.println("Total price: $" + total);
}
}
错误在第1,4,6,9,12,15,18,21,24,27,30,33和36行
谢谢你的帮助;我真的很感激!
答案 0 :(得分:0)
括号不是你唯一的问题。
public double calcCost() {
if (size.equals("small")) {
return 10 + 2 * (ham + cheese + pepperoni);
}
else if (size.equals("medium")) {
return 12 + 2 * (ham + cheese + pepperoni);
}
else if (size.equals("large")) {
return 14 + 2 * (ham + cheese + pepperoni);
}
else {
System.out.println("Invalid size");
}
}
这不会像else
那样编译,只返回指定返回类型时非法的内容。
至于括号,因为你给我们一行,并假设我们应该能够用你粘贴的代码正确计算它们。我相信你把所有这些都复制在一个文件中。
如果这是真的,那么问题在于前两个方法不在类中,这在Java中是非法的。要么将它们放在其他类中,要么将它们添加到已存在的类中(如果它们位于它们所在的位置)。
此外,您不应该在一个文件中声明所有类,即使这样编译也很难维护。
答案 1 :(得分:0)
待办事项
public class pizza{
public static pizzaSize(){
}
public static toppings(){
}
}
等等。如果你没有使方法无效,那么他们必须有回报,如果你让它们是静态的,那么你可以在没有即时katsina的情况下通过调用&#34; class.method()&#34;