非法启动Expression java

时间:2012-09-13 18:14:11

标签: java compiler-construction

我知道这对某人来说非常简单,我无法弄清楚为什么编译器抱怨这个。我一直在寻找一些答案,我能找到的只是一个支架问题,但我不认为这是我的问题。我是Java的新手,所以任何帮助都会很棒。这是应该是基本累加器程序的代码。

public class BasicAccumulator implements Accumulator {
  {
    private int digit;
    private int value;

  }

  public int basicAccumulator(int digit, int value)
  {
    digit = 0;
    value = 0;
  }

  public void addDigit(int digit);
  {
    digit = digit + value;
  }

  public void plus();
  {
    value = digit + digit;
  }

  public void minus();
  {
    value = digit - digit;
  }

  public void clear();
  {
    value = 0;
  }

  public int displayValue();
  {
    return value;
  }

}

5 个答案:

答案 0 :(得分:5)

public void plus();

删除分号。它应该是:

public void plus()
{ ...
}

同样适用于displayValue(),减去(),clear()。它应该是:

答案 1 :(得分:2)

我会直接在您的代码中发表我的评论:

public class BasicAccumulator implements Accumulator {

    //I'd delete this brackets and leave just the private declarations initialized
    //in zero.
    {
        private int digit;
        private int value;    
    }

    //I'm making this an initializing constructor by using the parameters
    //it defines. If you want both digit and value to be set to 0 (or any other value
    //by default) you can make a no argument constructor and invoke it.
    public BasicAccumulator(int digit, int value)
    {
        this.digit = digit;
        this.value = value;
    }

    public void addDigit(int digit); //This semicolon is wrong. Delete it.
    {
        digit = digit + value;
    }

    public void plus(); //This semicolon is wrong. Delete it.
    {
        value = digit + digit;
    }

    public void minus(); //This semicolon is wrong. Delete it.
    {
        value = digit - digit;
    }

    public void clear(); //This semicolon is wrong. Delete it.
    {
        value = 0;
    }

    public int displayValue(); //This semicolon is wrong. Delete it.
    {
        return value;
    }

}

我不知道这是一个例子还是其他什么,但逻辑也存在一些问题,但我会留给你(特别是minus方法,因为它总会设置值为0)。

答案 2 :(得分:0)

public void plus();
public void minus();
public void clear();
public int displayValue();

代码中的上述行是错误..

public void plus();
{
    value = digit + digit;
}

让它像这样......

public void plus()
{
    value = digit + digit;
}

对剩下的方法执行此操作.....

答案 3 :(得分:0)

在函数addDigit,plus,minus,clear和下面的两个签名后,那些分号是做什么的?删除它们。那将完成这项工作

答案 4 :(得分:0)

您发布的代码有三个问题。

  1. 你的所有方法都有分号。您使用它的唯一时间是您有一个抽象方法(在抽象类或接口中声明为abstract)。
  2. 您似乎尝试在初始化程序段中创建digitvalue。取下它们周围的支架。
  3. 构造函数不返回值,因此返回类型永远不会包含在其签名中。您有public int basicAccumulator但它应该只是public BasicAccumulator。另外,请注意区分大小写。
  4. 这是您的固定代码:

    public class BasicAccumulator implements Accumulator {
    
        private int digit;
        private int value;
    
        public BasicAccumulator(int digit, int value) {
            digit = 0;
            value = 0;
        }
    
        public void addDigit(int digit) {
            digit = digit + value;
        }
    
        public void plus() {
            value = digit + digit;
        }
    
        public void minus() {
            value = digit - digit;
        }
    
        public void clear() {
            value = 0;
        }
    
        public int displayValue() {
            return value;
        }
    }
    
相关问题