好的,所以我为测验程序制作了数组。虽然一切都结果还不错,但我对此部分感到困惑,以及确定我理解发生了什么。
这里是点击答案按钮时的代码(我只发布一个按钮的代码,因为它们都有相同的代码)
questionValueS = Integer.toString(questionValue);
questionInS = Integer.toString(questionIndex );
if(questionIndex < Questions.length -1
)
{
if (questionIndex != 5){
questionsLabel2.setText("");
} if (questionIndex == 5){
questionsLabel2.setText(halfQues8);
}
// make sure the index is in bounds
questionIndex++ ; // increment the index
questionValue ++;
QuestionIndexS.setText(questionInS);
questionsLabel.setText(Questions[questionIndex]); // set the text to the next question
/*
* set the text to the next answer options
*/
TextButtA.setText(AnswerButtA[questionIndex]);
TextButtB.setText(AnswerButtB[questionIndex]);
TextButtC.setText(AnswerButtC[questionIndex]);
questionNumber.setText("Question " + questionValueS + ".");
}
else{
questionsLabel.setText("Complete");
questionNumber.setText("");
questionsLabel2.setText("");
TextButtA.setText("");
TextButtB.setText("");
TextButtC.setText("");
Results.setText("Click here for results");
}
}
这是代码的另一半
*
*Icremented Variables
*/
Integer questionIndex = -1;
Integer questionValue = 2;
String questionValueS;
String questionInS;
//Non-incremented variable
String halfQues8 = "They got upset when he took ____ stuff.";
/*
*Questions Array
*/
String[] Questions =
{
"4 pears were on sale for $5. How much is each pear?", //Array 0//
"What is the temperature right below 0 degrees Fahrenheit?", //Array 1//
"How long it take to reach 60 miles if going 60mph?", //Array 2//
"How long does it take for Summer to get to the following Summer?", //Array 3
"What is 100% of 100?", //Array 4//
" What weighs more. 1 pound of feathers, or 1.01 pounds of thin aluminum foil?", //Array 5//
"Which word should be on the empty line?", //Array 6 //
"How many licks does it take to get to the tootsie roll center of a tootsie pop?(No biting)", //Array 7
"Which statement is true?", //Array 8
};
/*
* Button A
*/
String[] AnswerButtA = {
"2.25", //Array 0
"-1 Celcius", //Array 1
"2 hours", //Array 2
"12 months", //Array 3
"100", //Array 4
"1 pound of feathers" , //Array 5//
"their", //Array 6
"20", //Array 7
" The denser the cloud, the brighter Earth's atmosphere is." //Array 8
};
/*
* Button B
*/
String[] AnswerButtB = {
"1.25", //Array 0
"-1 Fahrenheit", //Array 1
" 1.5 hours", //Array 2
" 12 years", //Array 3
" 10,000", //Array 4
" 1.01 pounds of metal", //Array 5//
"there", //Array 6
"Less than 20 ", //Array 7
" The denser the cloud, the darker Earth's atmosphere is.", //Array 8
};
/*
* Button C
*/
String[] AnswerButtC = {"1.50", //Array 0
"1 Fahrenheit", //Array 1
"1 hour", //Array 2
"1.2 years", //Array3
"200", //Array 4
"They weigh the same", // Array 5//
"they're", // Array6 //
"More than 20", //Array 7
"Both A and B" //Array 8
};
作为一个抬头,我已经在gui本身上设置了第一个问题/答案,因此它没有存储在Array上。
因为我意识到我的代码有点草率我做了这个
questionInS = Integer.toString(questionIndex );
跟踪questionIndex值是什么。 我想知道为什么当questionsIndex值= -1时,它打印出Array [0]。数组编号随着问题一起增加,一切正常。但后来它让我想知道这句话是怎样的
TextButtA.setText(AnswerButtA[questionIndex]);
TextButtB.setText(AnswerButtB[questionIndex]);
TextButtC.setText(AnswerButtC[questionIndex]);
似乎是真的。当questionIndex值为-1而不是[0]时,如何打印出Array [0]?
答案 0 :(得分:2)
多种原因:
questionInS = Integer.toString(questionIndex);
if(questionIndex < Questions.length -1)
因此您的Questions
数组为空,如果增加questionIndex,则永远不会进入。所以你可能需要增加而不管条件是否增加它。