繁体   English   中英

Expression Java的非法开头

[英]Illegal start of Expression java

我知道这对于某人来说真的很简单,我不知道为什么编译器会抱怨这一点。 我一直在寻找一些答案,我只能找到一个括号问题,但我不认为这是我的问题。 我是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;
  }

}
public void plus();

删除分号。 它应该是:

public void plus()
{ ...
}

displayValue() ,minus(),clear()相同。 它应该是:

我将直接在您的代码中发布我的评论:

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)。

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

代码中的上述行是错误。

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

像这样...

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

对其余方法执行此操作。

在函数addDigit,加号,减号,clear和两个以下的分号后,这些分号做什么? 删除它们。 那将完成这项工作

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

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

这是您的固定代码:

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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM