我正在尝试编写一个简单的问答游戏。
尝试使用此代码示例布置一个问题和答案会话:
QuizCard(q, a){
String question = "What's the name of the lead actor in the movie 'I Robot'?";
String answer = "Will Smith";
q = question;
a = answer;
}
QuizCard是一个构造函数,但我得到一个标识符预期错误。有人可以让我明白我在做错了。
答案 0 :(得分:5)
您收到错误是因为您没有指定参数类型。您的构造函数也没有做任何事情,因为只更改了局部变量。听起来像你真正想要的是这样的:
public class QuizCard {
private String question;
private String answer;
public QuizCard(String question, String answer) {
this.question = question;
this.answer = answer;
}
}
答案 1 :(得分:-1)
QuizCard(String q, String a){
String question = "What's the name of the lead actor in the movie 'I Robot'?";
String answer = "Will Smith";
q = question;
a = answer;
}
注意:您错过了提供标识符,这意味着您要在构造函数中提供的参数类型。构造函数不知道您是否传递字符串或 int 或 double 。