1. 程式人生 > >終止迴圈(輸出100以內能被7整除的前5個數)

終止迴圈(輸出100以內能被7整除的前5個數)

輸出100以內能被7整除的前5個數 (1)while迴圈

public class DemoA{
	public static void main(String[] args) {
		int count = 0;
		int i = 1;
		//while迴圈實現
		while(i <= 100) {
			if(i%7 == 0) {
				System.out.println(i);
				count++;
			}
			i++;
			//提前終止迴圈
			if(count == 5) {
				break;
			}
		}
	}
}

(2)for迴圈實現

public class DemoB {
	public static void main(String[] args) {
        int count2 = 0;
		for(int i1 = 1;i1<=100;i1++) {
			if(i1 % 7 == 0) {
				System.out.println(i1);
				count2++;
			}
			if(count2 == 5) {
				break;
			}
		}
	}
}