Java 如何每三个数字打印一次?
如何每三个数字打印一次 我知道这将打印出从0到100的数字:Java 如何每三个数字打印一次?,java,while-loop,java-8,Java,While Loop,Java 8,如何每三个数字打印一次 我知道这将打印出从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
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++;
}
}
}
尝试:
int i = 1;
while (i++ <= 100){
if ( i % 3 == 0 ) {
System.out.println(i);
}
}
增加3
private static void countNumber(){
// This is the whilestatement.
int i = 3;
while(i <=100){
System.out.println(i);
i+=3;
}
}
用于:
输出:
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99
这个循环不好。但不改变你的流量。只需添加一个条件来统计您想要的内容
int i = 0;
while (i <= 100) {
if (i != 0 && i % 3 == 0)
System.out.println(i);
i++;
}
从i=3开始,每轮加3?System.out.printlni*3@Ingo那你应该@ᴍ阿伦ᴍaroun是的,或者我*3@ᴍ阿伦ᴍ最后一个例子只是说明问题可以通过多种方式解决-
int i = 3;
while(i <=100){
System.out.println(i);
i += 3;
}
private static void countNumber(){
// This is the whilestatement.
int i = 3;
while(i <=100){
System.out.println(i);
i+=3;
}
}
for (int i = 3; i <= 100; i+=3)
{
System.out.println(i);
}
IntStream.iterate(3, x -> x + 3).limit(100/3).forEach(System.out::println);
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99
int i = 0;
while (i <= 100) {
if (i != 0 && i % 3 == 0)
System.out.println(i);
i++;
}
public static void main(String[] args) {
for(int i=3; i < 100; i = i + 3) {
System.out.println(i);
}
}