如何使每3个数字打印出一个while语句?
我知道这将打印出从0到100的数字:
package WhileStatement;
import java.util.Scanner;
public class WhileStatementOne {
//See the Scanner packet for more info.
static Scanner input = new Scanner(System.in);
public static void main(String[] args){
countNumber();
}
//We can now call ' countNumber ' at any time in this code.
private static void countNumber(){
// This is the whilestatement.
int i = 0;
while(i <=100){
System.out.println(i);
i++;
}
}
}
但是我怎么能把它打印出来呢:
3
6
9
12
15
etc...
尝试:
int i = 1;
while (i++ <= 100){
if ( i % 3 == 0 ) {
System.out.println(i);
}
}
或者:
int i = 3;
while (i <= 100){
System.out.println(i);
i += 3;
}
甚至:
for (int i : new Range(3,100,3)) {
System.out.println(i);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句