- 语法错误,插入"}"以完成方法主体



我花了几个小时试图解决这个语法错误,但它在各个方面都击败了我。我尝试在末尾放置右括号,在各个块之间,但没有任何效果。这是针对课堂作业的,它几乎完成了,但此错误使我无法实际提交它。

public class Coffee {
public static void main(String[] args) {
// Default Constructor 
public Coffee() {
this.sugar = 0.0;
this.milk = 0;
this.currentlyHot = false;
}  
//Instance Variables        
private double sugar;
private int milk;
private boolean currentlyHot; // hot
private int size;
// Constructor        
public Coffee (double id, int dairy, boolean temp) {
this.sugar = id;
this.milk = dairy;
this.currentlyHot = temp;        
}
// (setter)
public void setSugar(double id) {
sugar = id;
}
public void setMilk(int dairy) {
milk = dairy;
}       
public void setSize(boolean temp) {
currentlyHot = temp;
}
public void setHeat(boolean isHot) {
this.currentlyHot = isHot;
}
//(getter)  
public double getSugar() {
return this.sugar;
}
public int getMilk() {
return this.milk;
}
public boolean checkcurrentlyHot() {
return this.currentlyHot;
}
public int getSize() {
return this.size;
}
// Method to display data
public void display() {
System.out.println("You have " + sugar + " sugar in your cup");
System.out.println("You have " + getMilk() + " in your coffee");
System.out.println("That's a " + getSize() + " ounce cup");
System.out.println("Is the cup hot? " + checkcurrentlyHot());            

}
}

你不能在你的main中拥有你的构造函数和其他代码

public static void main(String[] args) {
// Default Constructor 
public Coffee() {   // move this code
....
}
....
}

您可能希望将main作为

public static void main(String[] args) {
Coffee c = new Coffee ();  // or use the other constructor
c.display ();
}

你已经把所有东西都放在了主方法中。构造函数、属性声明、方法声明。最后还有一个缺少端部大括号。将构造函数、变量和方法声明移出 main 方法。

实例方法和变量在main((中声明,这在Java中是不可能的。把它从主上拿出来,它会起作用的。

class Coffee {
public Coffee() {
this.sugar = 0.0;
this.milk = 0;
this.currentlyHot = false;
}  
//Instance Variables        
private double sugar;
private int milk;
private boolean currentlyHot; // hot
private int size;
// Constructor        
public Coffee (double id, int dairy, boolean temp) {
this.sugar = id;
this.milk = dairy;
this.currentlyHot = temp;        
}
// (setter)
public void setSugar(double id) {
sugar = id;
}
public void setMilk(int dairy) {
milk = dairy;
}       
public void setSize(boolean temp) {
currentlyHot = temp;
}
public void setHeat(boolean isHot) {
this.currentlyHot = isHot;
}
//(getter)  
public double getSugar() {
return this.sugar;
}
public int getMilk() {
return this.milk;
}
public boolean checkcurrentlyHot() {
return this.currentlyHot;
}
public int getSize() {
return this.size;
}
// Method to display data
public void display() {
System.out.println("You have " + sugar + " sugar in your cup");
System.out.println("You have " + getMilk() + " in your coffee");
System.out.println("That's a " + getSize() + " ounce cup");
System.out.println("Is the cup hot? " + checkcurrentlyHot());            

}
public static void main(String[] args) {
// Default Constructor 

}
} 

最新更新