我想将这些方法放入ArrayList中,但我无法弄清楚如何做到这一点。我正在使用NetBeans,该程序将成为一个GUI。
这是我的代码:
public class canada150Tour extends javax.swing.JFrame {
ArrayList <String> mcQuestions = new ArrayList(); //This is the array I want to put the below methods into.
private void questionFredricton(){
outputTextQuestion.append("Where is Fredricton located?");
displayQuestions.append("New Brunswick");
}
private void questionRegina(){
outputTextQuestion.setText("Where is Regina located?");
displayQuestions.append("Saskatchewan");
}
private void questionOttawa(){
outputTextQuestion.setText("Where is Ottawa located?");
displayQuestions.append("Ontario");
}
private void questionHalifax(){
outputTextQuestion.setText("Where is Halifax located?");
displayQuestions.append("Nova Scotia");
}
private void questionQuebecCity(){
outputTextQuestion.setText("Where is Quebec City located?");
displayQuestions.append("Quebec");
}
private void questionEdmonton(){
outputTextQuestion.setText("Where is Edmonton located?");
displayQuestions.append("Alberta");
}
private void questionCharlottetown(){
outputTextQuestion.setText("Where is Charlottetown located?");
displayQuestions.append("Prince Edward Island");
}
private void questionVictoria(){
outputTextQuestion.setText("Where is Victoria located?");
displayQuestions.append("British Columbia");
}
private void questionIqaluit(){
outputTextQuestion.setText("Where is Iqaluit located?");
displayQuestions.append("Nunavut");
}
private void questionWhitehorse(){
//outputTextQuestion.setText("Where is Whitehorse located?");
displayQuestions.append("Yukon");
}
private void questionWinnipeg(){
outputTextQuestion.setText("Where is Winnipeg located?");
displayQuestions.append("Manitoba");
}
我试过了:
Collections.addAll(mcQuestions, questionFredricton(), questionRegina());
等等所有方法,但显然不起作用。
当我尝试添加到ArrayList(上面)时出现编译错误:
这里不允许'void'类型
我对Java很新,所以还在学习。
修改 这最终将是一个多项选择测验。我基本上想把方法放到一个数组中,这样我就可以随机选择方法。
提前致谢!
答案 0 :(得分:0)
您需要将ArrayList
作为参数传递给方法,以便在其中添加元素,例如:
private void questionFredricton(ArrayList <String> mcQuestions){
mcQuestions.add("Where is Fredricton located?");
}
并调用这样的方法:
ArrayList <String> mcQuestions = new ArrayList();
questionFredricton(mcQuestions);
此外,您似乎需要在此处存储这对问题和答案,在这种情况下,最好使用HashMap
,例如:
private void questionFredricton(Map<String, String> questions){
questions.put("Where is Fredricton located?", "New Brunswick");
}
并称之为:
Map<String, String> questions = new HashMap<>();
questionFredricton(questions);
这样,您就可以存储问题和答案。 questions.keySet()
会为您提供所有问题的清单,questions.get("<questions>")
会给您答案。
答案 1 :(得分:0)
如果要在String
String
ArrayList
中添加String
,您的方法应返回QuestionAndResponse
。
但实际上我认为你不应该这样做,因为你需要问题和回答。拥有List
而不是两个QuestionAndResponse
字符串的列表似乎更好
public class QuestionAndResponse{
public QuestionAndResponse(String question, String response){
this.question = question;
this.response = response;
}
// getters for fields
}
可以包含问题的文本,但也可以包含预期响应的文本。
List <QuestionAndResponse> mcQuestionsResponses = new ArrayList<>();
所以你可以声明List:
private void questionFredricton(){
outputTextQuestion.append("Where is Fredricton located?");
displayQuestions.append("New Brunswick");
}
你可以替换它:
private QuestionAndResponse questionFredricton(){
return new QuestionAndResponse("Where is Fredricton located?","New Brunswick");
}
由:
public class Canada150Tour extends JFrame {
List <QuestionAndResponse> mcQuestionsResponses;
...
public Canada150Tour(){
mcQuestionsResponses = new ArrayList<>();
mcQuestionsResponses.add(questionFredricton());
}
private QuestionAndResponse questionFredricton(){
return new QuestionAndResponse("Where is Fredricton located?",
"New Brunswick");
}
}
现在您可以使用Canada150Tour(请注意遵循命名约定的第一个字母的大写)构造函数:
seaborn.set(style="white", context="talk")
a = pandas.DataFrame.from_dict({'Attendees': {pandas.Timestamp('2016-12-01'): 10,
pandas.Timestamp('2017-01-01'): 12,
pandas.Timestamp('2017-02-01'): 15,
pandas.Timestamp('2017-03-01'): 16,
pandas.Timestamp('2017-04-01'): 20}})
ax = seaborn.barplot(data=a, x=a.index, y=a.Attendees, color='lightblue', )
seaborn.despine(offset=10, trim=False)
ax.set_ylabel("")
ax.set_xticklabels(['Dec', 'Jan','Feb','Mar','Apr'])
plt.show()