好的,所以我为测验程序制作了数组。尽管一切正常,但我对这部分内容以及确保我了解发生了什么感到困惑。
这是单击“答案”按钮时的代码(我只发布了一个按钮的代码,因为它们都具有相同的代码)
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]?
最佳答案
多个原因:
在增加questionIndex之前会调用questionInS = Integer.toString(questionIndex);
if(questionIndex < Questions.length -1)
,因此您的Questions
数组为空,并且您从不考虑是否要增加questionIndex。因此,无论条件如何,都可能需要增加它。
关于java - 为什么会这样呢? (使用数组,增量),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28634559/