表达式 java 的非法启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12412326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:47:44  来源:igfitidea点击:

Illegal start of Expression java

javacompiler-construction

提问by Eric Sage

I know this is going to be really simple for someone and I can't figure out why the compiler is complaining about this. I have been looking up for some answers and all I can find is a bracket issue but I don't think that is my problem. I am new to Java so any help would be awesome. This is the code that is supposed to be a basic accumulator program.

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

}

回答by kosa

public void plus();

remove semi-colon. It should be:

删除分号。它应该是:

public void plus()
{ ...
}

Same for displayValue(),minus(), clear(), also. It should be:

同为displayValue(),减(),明确的(),也。它应该是:

回答by Fritz

I'll post my comments directly in your code:

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

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

}

I don't know if this was an example or anything but there are some issues with the logic too, but I'll leave those to you (the minusmethod in particular, since it will always set the value to 0).

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

回答by Kumar Vivek Mitra

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

The above lines in code is the error..

上面的代码行是错误..

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

Let it be like this...

让它变成这样...

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

Do this for the remaining methods.....

对其余方法执行此操作.....

回答by Shades88

what are those semicolons doing after signatures of functions addDigit, plus, minus, clear and two below that? Remove them. That will do the job

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

回答by Brian

Your posted code has three problems.

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

  1. You have semi-colons after all your methods. The only time you use this is when you have an abstract method (declared abstractin an abstract class or in an interface).
  2. It looks like you tried to create digitand valuein an initializer block. Remove the brackets around them.
  3. Constructors do not return a value, so the return type is never included in its signature. You have public int basicAccumulatorbut it should be just public BasicAccumulator. Also, keep an eye on case-sensitivity.
  1. 在所有方法之后都有分号。唯一使用它的时候是当你有一个抽象方法(abstract在抽象类或接口中声明)。
  2. 看起来您尝试在初始化程序块中创建digitvalue。取下它们周围的括号。
  3. 构造函数不返回值,因此返回类型永远不会包含在其签名中。你有,public int basicAccumulator但它应该只是public BasicAccumulator. 另外,请注意区分大小写。

Here is your fixed code:

这是您的固定代码:

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