java循環結構,while,do while,for

while(先判斷再執行)
public class foreach {
	public static void main(String[] args) {
		// 
		boolean show = true;
		int i = 0;
		while(show) {
			i++;//每次加一
			System.out.println(i);
			if(i == 3) {
				show = false;
			}
		}
		
		int j = 0;
		while(true) {
			j++;//每次加一
			System.out.println(j);
			if(i == 3) {
				break;
			}
		}
	}

}
do while(先執行再判斷)
public class foreach {

	public static void main(String[] args) {
		int n = 1;
		int sum = 0;
		do {
			sum += n;
			n++;
		} while(n <= 100);
		System.out.println(sum); //5050
	}

}
for
public class foreach {

	public static void main(String[] args) {
		int sum = 0;
		for(int i = 1;i<=100;i++){
			sum += i;
		}
		System.out.println(sum);
	}
}
foreach
public class foreach {

	public static void main(String[] args) {
		String[] arr = {"你","算","什","麼","蝦","米"};
		for(String item:arr){
			System.out.println(item);
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章