[英]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
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.