【发布时间】:2018-10-23 14:31:03
【问题描述】:
我正在创建一个基于 Java GUI 的程序,但我有一系列问题。在单击JButton 后,我需要创建一个获取下一个问题的方法。下一个问题应随机选择,不应重复先前提出的问题。请看下面的代码——getNextQuestion 将如何实现?
public class QuestionBank {
String [] questions;
int currQuestionIndex;
public QuestionBank() {
questions = new String [10]; //increase array size if you will add more questions
questions[0]= "Which three words describe you best?";
questions[1]= "Which is your best feature?";
questions[2]= "Which common saying or phrase describes you?";
questions[3]= "What’s the best thing that’s happened to you this week?";
questions[4]= "Who was your role model when you were a child?";
questions[5]= "Who was your favorite teacher and why?";
questions[6]= "What was your favorite subject at school?";
questions[7]= "What did you want to be when you grew up?";
questions[8]= "If you could have one wish come true what would it be?";
questions[9]= "Which would you prefer — three wishes over five years or one wish right now?";
//add more questions
}
public String getNextQuestion() {
//get the next question, randomly from the array and without repeats
}
}
【问题讨论】:
-
问题可以重复吗?
-
递增
currQuestionIndex并验证它没有超出范围... -
@MadProgrammer - 你错过了'从数组中随机...'
-
@zlakad 不同的是?!很抱歉很难看到问题
-
@MadProgrammer 检查 OP 的源代码注释
标签: java