简体   繁体   English

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

[英]How to run a loop over certain numbers only

I wanted to run a for loop for 1,7,14,19.I know this is a basic question but I could not get the idea.I tried with 我想为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{

     }          
 }

But this keeps on printing.Also same with below code 但这一直在打印。下面的代码也一样

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

Any help is appreciated. 任何帮助表示赞赏。

I use an array 我用一个数组

for (int i : new int[] { 1, 7, 14, 19 }) {
    // something with i

In Java 8+, you could use an IntStream . 在Java 8+中,可以使用IntStream Like, 喜欢,

IntStream.of(1, 7, 14, 19).forEachOrdered(System.out::println);

Use Array and for each 对每个使用Array

int ary[]= { 1, 7, 14, 19} ;
for(int i : ary){
System.out.println(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当char数组包含某些字符时,如何才能使此循环运行? - How can i make this loop run only if the char array contains certain characters? 如何仅使用for循环生成奇数 - How to produce odd numbers using only a for loop 一个循环,询问一定数量的数字,并输出其中有多少是偶数 - A loop that asks for a certain amount of numbers and outputs the how many of them are even 如何按字典顺序遍历所有特定长度的可能向量? - How to loop over all possible vectors of certain length in lexicographic order? Foreach在循环的某些部分 - Foreach over certain part of a loop for循环似乎只在应该运行直到达到特定值时才运行一次 - for-loop seems to only run once when it should run until certain value reached 如何在不控制运行方法循环的情况下挂起线程 - How to suspend a thread without control over the loop in run method 如何仅使用foreach循环的第一个输出来执行某些任务? - How to do a certain task only with the first output of a foreach loop? 使用Spring和@IfProfileValue如何获取仅要运行的特定方法 - Using Spring and @IfProfileValue how to get only a certain method to run 如何在Java-8中仅并行运行某些中间操作? - How to run only certain intermediate operations in parallel in Java-8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM