英文:
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 ";", , expected
) in the third line (private String text = "a"
), 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= "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);
}
}
答案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= "a";
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= "a";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= "a", 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 "11"
and not the int 2
?
private String text= "a", password="1"+"1";
答案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= "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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论