首页 > 解决方案 > 初学者:Java 给我一个错误(短代码)令牌 ";", 上的语法错误,预期

问题描述

代码的工作方式如下Syntax error on token ";", , expected,在第三行 ( private String text = "a") 中给了我一个错误 ( ),当我将下一行设为注释而不是代码时,该错误就会消失。
此外,这是为了“加密”字符串,我没有使用注释,因为它实际上只有几行。

public class Ebook {
    private String password;
    private String text= "a";password=(char)1+(char)1;
    //                      ^ here
        
    public void setText(String text) {
        this.text = "";
        char a = text.charAt(1);
        for (int i = 0; i < text.length(); i++) {
            a = (char) (text.charAt(i) + this.password.charAt((i % password.length())));
            this.text = this.text + a;
        }
        System.out.println(this.text);
    }
}

标签: javasyntax

解决方案


你必须这样做 -

private String password = (char)1+(char)1;
private String text= "a";

Java 不允许在类上再次初始化。


推荐阅读