为什么会这样? (使用数组,增量)
Why is this happening? (Using arrays, increments)
好的,所以我为测验程序制作了数组。虽然一切都很好,但我对这部分感到困惑,以及如何确保我理解发生了什么。
这是点击回答按钮时的代码(我只 post 一个按钮的代码,因为它们都有相同的代码)
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 . 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 本身上设置了第一个 question/answer,所以它没有存储在数组中。
因为我意识到我的代码有点草率所以我做了这个
questionInS = Integer.toString(questionIndex );
跟踪问题索引值是什么。
我想知道为什么当 questionsIndex 值 = -1 时,它会打印出 Array[0]。数组编号确实随着问题的增加而增加,一切正常。但是这让我想知道这个声明
TextButtA.setText(AnswerButtA[questionIndex]);
TextButtB.setText(AnswerButtB[questionIndex]);
TextButtC.setText(AnswerButtC[questionIndex]);
似乎是真的。当 questionIndex 值为 -1 而不是 [0] 时,如何打印出 Array[0]?
多种原因:
questionInS = Integer.toString(questionIndex);
在递增 questionIndex 之前被调用
if(questionIndex < Questions.length -1)
所以你的 Questions
数组是空的,你永远不会进入 if 来增加问题索引。所以你可能需要增加而不考虑条件来增加它。
好的,所以我为测验程序制作了数组。虽然一切都很好,但我对这部分感到困惑,以及如何确保我理解发生了什么。
这是点击回答按钮时的代码(我只 post 一个按钮的代码,因为它们都有相同的代码)
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 . 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 本身上设置了第一个 question/answer,所以它没有存储在数组中。
因为我意识到我的代码有点草率所以我做了这个
questionInS = Integer.toString(questionIndex );
跟踪问题索引值是什么。 我想知道为什么当 questionsIndex 值 = -1 时,它会打印出 Array[0]。数组编号确实随着问题的增加而增加,一切正常。但是这让我想知道这个声明
TextButtA.setText(AnswerButtA[questionIndex]);
TextButtB.setText(AnswerButtB[questionIndex]);
TextButtC.setText(AnswerButtC[questionIndex]);
似乎是真的。当 questionIndex 值为 -1 而不是 [0] 时,如何打印出 Array[0]?
多种原因:
questionInS = Integer.toString(questionIndex);
在递增 questionIndex 之前被调用
if(questionIndex < Questions.length -1)
所以你的Questions
数组是空的,你永远不会进入 if 来增加问题索引。所以你可能需要增加而不考虑条件来增加它。