- 语法错误,插入"}"完成MethodBody

时间:2017-07-24 02:55:10

标签: java syntax-error

我花了几个小时试图解决这个语法错误,但它在各方面都打败了我。我尝试在各个块之间放置结束括号,但没有任何方法可行。这是一个课堂作业,它已经完成了很多但是这个错误使我无法实际提交它。

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());            


        }

    }

3 个答案:

答案 0 :(得分:2)

您的main menthod

中不能包含构造函数和其他代码
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 ();
}

答案 1 :(得分:0)

实例方法和变量在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 


    }
} 

答案 2 :(得分:0)

您已将所有内容都放在main方法中。构造函数,属性声明,方法声明。最后还有一个缺少的末端支撑。将构造函数,变量和方法声明移出main方法。