Java给我一个错误(简短的代码)Syntax error on token “;”,期望有一个逗号。

huangapple go评论73阅读模式
英文:

Beginner: Java is giving me an error (short code) Syntax error on token ";", , expected

问题

以下是翻译好的部分:

public class Ebook {
    private String password;
    private String text = "a";  // 这里出现错误 (`Syntax error on token ";", , expected`)

	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);
    }
}
英文:

The code works as following giving me an error (Syntax error on token &quot;;&quot;, , expected) in the third line (private String text = &quot;a&quot;), which disappears when I make the following line an comment instead of code.
Also, this is meant to "encrypt" the String, and I haven't used comments, as it is only a few lines really.

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

答案1

得分: 1

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

Java不允许在类上重新进行初始化。

英文:

You have to do it like -

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

Java doesn't allow to initialize again on the class.

答案2

得分: 1

有关这些行的问题有三个:

private String password;
private String text= "a";password=(char)1+(char)1;
  • 两个字段不能拥有相同的名称。
  • 在同一行上初始化字段必须用逗号分隔。
  • 类型不匹配:int 不能(自动)转换为 String。

要在一行中解决这三个问题,您可以执行以下操作:

private String text= "a", password=Integer.toString((char)1+(char)1);
  • password 仅被初始化一次。
  • 字段由逗号分隔。
  • 并且 int 2 被转换为 String。

但是,password 的值可能应该是 String "11",而不是 int 2

private String text= "a", password="1"+"1";
英文:

There are three problems with the lines:

private String password;
private String text= &quot;a&quot;;password=(char)1+(char)1;
  • two fields can not have the same name.
  • initializing fields on the same line must be separated by comma
  • type mismatch: an int can not be (automatically) converted to String

To fix all three issues in one line, you could do the following:

private String text= &quot;a&quot;, password=Integer.toString((char)1+(char)1);
  • password is only initialized once.
  • the fields are separated by comma
  • and the int 2 is converted to String

But the value of password should probably be the String &quot;11&quot; and not the int 2?

private String text= &quot;a&quot;, password=&quot;1&quot;+&quot;1&quot;;

答案3

得分: 0

(char) 1 + (char) 1 的结果是一个整数然后您尝试将其保存到一个字符串中以下代码将起作用

public class Ebook{
private String password =  Integer.toString((char)1 + (char) 1);
private String text= "a";

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);
}
}
英文:

(char) 1 + (char) 1 results in an integer, which you are then trying to save into a String. The following code would work:

public class Ebook{
private String password =  Integer.toString((char)1 + (char) 1);
private String text= &quot;a&quot;;


public void setText(String text) {

    this.text=&quot;&quot;;
    char a =text.charAt(1);
    for (int i = 0; i&lt; text.length(); i++) {
        a= (char) (text.charAt(i) + this.password.charAt((i%password.length())));
        this.text = this.text + a;
        
    }
System.out.println(this.text);
}
}

huangapple
  • 本文由 发表于 2020年10月16日 17:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64386918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定