有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么会发生这种情况?(使用数组、增量)

好的,所以我制作了一个测验程序的数组。虽然一切都很顺利,但我对这一部分以及如何确保我了解发生了什么感到困惑

下面是点击回答按钮时的代码(我只发布一个按钮的代码,因为它们都有相同的代码)

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本身上设置了第一个问题/答案,因此它不存储在数组中

因为我意识到我的代码有点马虎,所以我做了这个

questionInS = Integer.toString(questionIndex );   

跟踪questionIndex值是什么。 我想知道为什么当questionsIndex值=-1时,它会打印出数组[0]。数组的数量随着问题的增加而增加,一切都正常。但这让我想知道这句话是怎么说的

    TextButtA.setText(AnswerButtA[questionIndex]);
    TextButtB.setText(AnswerButtB[questionIndex]);
    TextButtC.setText(AnswerButtC[questionIndex]);

似乎是真的。当questionIndex值为-1而不是[0]时,如何打印出数组[0]


共 (1) 个答案

  1. # 1 楼答案

    多重原因:

    • ^在递增索引之前调用{}
    • if(questionIndex < Questions.length -1)所以你的Questions数组是空的,你永远不会进入if来增加questionIndex。因此,您可能需要在不考虑条件的情况下增加它