如何仅对某些数字运行循环

RKR*_*RKR 0 java loops for-loop

我想为1,7,14,19运行for循环.我知道这是一个基本问题,但我无法理解.我试过

 for(int i=1;;i++){
     if(i==1||i==7||i==14||i==19){
         System.out.println(i);
     } else if(i==20){
         break;
     } else{

     }          
 }
Run Code Online (Sandbox Code Playgroud)

但这继续打印.也与下面的代码相同

for(int i=1;(i==1||i==7||i==14||i==19);i++){
    System.out.println(i);      
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

Pet*_*rey 11

我用一个数组

for (int i : new int[] { 1, 7, 14, 19 }) {
    // something with i
Run Code Online (Sandbox Code Playgroud)


Ell*_*sch 5

在 Java 8+ 中,您可以使用IntStream. 喜欢,

IntStream.of(1, 7, 14, 19).forEachOrdered(System.out::println);
Run Code Online (Sandbox Code Playgroud)