如何用3个更改的值将此语句循环到40个文本字段的setText中?

时间:2018-10-28 06:47:02

标签: java loops puzzle

这是我没有存储在列表中时的代码。这得到了我想要显示在不同文本字段中的内容,但是我希望它更短一些,所以我想循环它。

//“ answerStoration.retrieveDataChoices(i,TB)”是其他类的函数,该函数返回一个数组列表;

<html>
   <head>
      <title>JS Operator</title>
   </head>
   <body>
      <input id="num1" type="number" placeholder="Enter first number" name="num1"/> <br/><br/>
      <input id="num2" type="number" placeholder="Enter second number" name="num2"/> <br/><br/>
      <input id="op" type="text" placeholder="Enter operator" name="op"/> <br/><br/>
      <button id="calculate" name="calculate"> Calculate </button><br/>
      <p id="result">Result: </p>
   </body>
</html>

我将其存储在列表“ quizAnswerSTORE”中,但我尝试循环但不起作用。

quizAnswer1store.setText(answerStoration.retrieveDataChoices(1,TB).get(0));
            quizAnswer2store.setText(answerStoration.retrieveDataChoices(1,TB).get(1));
            quizAnswer3store.setText(answerStoration.retrieveDataChoices(1,TB).get(2));
            quizAnswer4store.setText(answerStoration.retrieveDataChoices(1,TB).get(3));
            quizAnswer1store2.setText(answerStoration.retrieveDataChoices(2,TB).get(0));
            quizAnswer2store2.setText(answerStoration.retrieveDataChoices(2,TB).get(1));
            quizAnswer3store2.setText(answerStoration.retrieveDataChoices(2,TB).get(2));
            quizAnswer4store2.setText(answerStoration.retrieveDataChoices(2,TB).get(3));
            quizAnswer1store3.setText(answerStoration.retrieveDataChoices(3,TB).get(0));
            quizAnswer2store3.setText(answerStoration.retrieveDataChoices(3,TB).get(1));
            quizAnswer3store3.setText(answerStoration.retrieveDataChoices(3,TB).get(2));
            quizAnswer4store3.setText(answerStoration.retrieveDataChoices(3,TB).get(3));

预期结果是在40个txt字段中显示来自数据库的不同值。因为每次循环值增加,它就会以不同的值滚动浏览我的数据库。 J变量表示数据库中的ID。 K是四列数据库中retrieveDataAnswers函数返回的arrayList中所取值的索引。

你去了。希望您能解决这个问题。

2 个答案:

答案 0 :(得分:1)

您可以使用mod来控制最大整数值,例如i % 10的值不能超过10。

示例:

public class Main {
    public static void main(String[] args) {
        int j = 1;
        int k = 0;


        for(int i = 0; i < 40; i++) {

            System.out.println("quizAnswerSTORE"+i+".setText(answerStoration.retrieveDataChoices("+j+",TB).get("+k+"));");
            k = (k + 1)%4;

            if( k == 0) {
                j = (j+1) % 11;
            }

        }

    }
}

输出:

quizAnswerSTORE0.setText(answerStoration.retrieveDataChoices(1,TB).get(0));
quizAnswerSTORE1.setText(answerStoration.retrieveDataChoices(1,TB).get(1));
quizAnswerSTORE2.setText(answerStoration.retrieveDataChoices(1,TB).get(2));
quizAnswerSTORE3.setText(answerStoration.retrieveDataChoices(1,TB).get(3));
quizAnswerSTORE4.setText(answerStoration.retrieveDataChoices(2,TB).get(0));
quizAnswerSTORE5.setText(answerStoration.retrieveDataChoices(2,TB).get(1));
quizAnswerSTORE6.setText(answerStoration.retrieveDataChoices(2,TB).get(2));
quizAnswerSTORE7.setText(answerStoration.retrieveDataChoices(2,TB).get(3));
quizAnswerSTORE8.setText(answerStoration.retrieveDataChoices(3,TB).get(0));
quizAnswerSTORE9.setText(answerStoration.retrieveDataChoices(3,TB).get(1));
quizAnswerSTORE10.setText(answerStoration.retrieveDataChoices(3,TB).get(2));
quizAnswerSTORE11.setText(answerStoration.retrieveDataChoices(3,TB).get(3));
...
quizAnswerSTORE38.setText(answerStoration.retrieveDataChoices(10,TB).get(2));
quizAnswerSTORE39.setText(answerStoration.retrieveDataChoices(10,TB).get(3));

答案 1 :(得分:0)

请尝试与您的缩进保持一致,并在括号“}”中加上相应的语句。

我在这段代码中看到的第一个问题是k不会在while循环内递增,因此它将始终具有相同的值并永远循环。我看到的第二个问题是,while循环后k不会重置,因此当它第一次通过循环(并正确递增)时,它将保持为4的值,此后每次都会跳过循环。

我不确定您要实现的目标(我可以使用一些更多信息或示例输出),但首先可以通过将while循环更改为for循环来纠正循环错误。 / p>

for (int i = 0; i < quizAnswerSTORE.size(); i++) {
    for (int j = 1; j < 11; j++) {
        for (int k = 0; k < 4; k++) {
            quizAnswerSTORE.get(i).setText(answerStoration.retrieveDataChoices(j,TB).get(k));
        }
    }
}

或者,如果您想保留while循环,可以这样。

for (int i = 0; i < quizAnswerSTORE.size(); i++) {
    for (int j = 1; j < 11; j++) {
        int k = 0; // Set k inside the 2nd loop and it will reset to 0 after the while loop
        while(k < 4) {
            quizAnswerSTORE.get(i).setText(answerStoration.retrieveDataChoices(j,TB).get(k));
            k++; // Shorthand for k += 1 which is shorthand for k = k + 1
        }
    }
}