在百里香的循环

use*_*645 14 html each loops thymeleaf

我该怎么做(java):

for(int i = 0; i < 81 ; i+=20){
   //Should loop through 5 times!
}
Run Code Online (Sandbox Code Playgroud)

在Thymeleaf?

我试过这个:

<option th:each="i : ${#numbers.sequence( 1, 81/20)}">
   <p th:text="${ i }"></p> <!-- THIS loops 4 times, instead of 5 -->
</option>
Run Code Online (Sandbox Code Playgroud)

问题是它不像java代码那么准确.这该怎么做?

win*_*ndX 10

在代码中添加步骤非常简单.

#{numbers.sequence(0, 81, 20)}
Run Code Online (Sandbox Code Playgroud)


sit*_*ant 9

使用iterStat关键字进行迭代.示例如果您有一个String数组,并且使用百日咳进行迭代.

<div th:each="str,iterStat : strings">
   <span th:text="${str}"/><!--This will print the index value-->
   <span th:text="${iterStat.index}"/><!--This will print the Index-->
</div> 
Run Code Online (Sandbox Code Playgroud)


小智 6

第一个值是计数的开始值,第二个值是最大值,第三个值是增量值

${#numbers.sequence( 1, 4, 1)}
Run Code Online (Sandbox Code Playgroud)


Jaa*_*dik 5

我假设这是由于您使用的数字.对于你的java代码,int i = 0; 我<81; i + = 20将返回i = 0,i = 20,i = 40,i = 60且i = 80

但是你的下面的代码number.sequence(1,81/20)}应该返回从1到4.05的整数,分别为1,2,3和4.

第一个循环返回i的5个结果,因此运行5次.第二个只返回4个结果,因此运行4次.我建议从0开始运行你的序列,根据需要返回5个结果.

如果您希望Java代码镜像第二个代码,则应将其更改为:int i = 1; 我<4.05; I + = 1

简单地说,你正在运行一个具有不同数字的循环,我建议将第二个语句改为从0开始.