1. 程式人生 > >java基礎(一) for / while / do...while迴圈語句

java基礎(一) for / while / do...while迴圈語句

開始學習jva基礎,做此筆記
三種迴圈的區別:

  1. 對於知道明確區間的需求,比如輸出1-10之間的奇數使用for迴圈
  2. 不明確區間的使用while
  3. 使用for迴圈,迴圈一結束變數就停止佔用記憶體
  4. 初始化變數如果在迴圈外還需要用建議用while
  5. do while迴圈表示此迴圈至少執行一次,for/while迴圈可以一次都不執行

eg:

for迴圈語句:

package com.st.basis.day04;

/*
 * for迴圈語句:
 * 格式:
 * 
 * for(初始化語句;判段語句;控制條件語句){
 *           迴圈體語句
 *	}
 * */
public class ForDome01 { //偶數和 public static Object shuchu(){ int a = 0; for(int i=1; i<=100; i++){ // a = a + i; if(i%2==0){ a += i; } } return a; } //階乘 public static Object jiecheng(int data){ int n = data; int sum = 1; for(int i=1; i<=n; i++){ sum *= i; } return
sum; } public static Object demo01(int data){ int n = data; int sum = 1; for(int i=1; i<=n-1; i++){ int a = n-i; int c = n*a; sum *= c; } return sum; } //水仙花數(100-1000) //暴力法 /*水仙花數是一個三位數,每個位的數字的3次冪之和等於它本身。 * eg:153 * 1^3 + 5^3+ 3^3 = 153 */ public static Object demo02(int
data){ System.out.println("共有下面幾位:"); for(int i=99; i<1000; i++){ int baiwei = i/100; int shiwei = i/10%10; int gewei = i%10; int sum = (int) (Math.pow(baiwei, 3) + Math.pow(shiwei, 3) + Math.pow(gewei, 3)); if(sum == i){ System.out.println(sum); } } return data; } //2.0 public static void demo03(){ System.out.println("共有下面幾位:"); for(int i=99; i<1000; i++){ int baiwei = i/100; int shiwei = i/10%10; int gewei = i%10; int sum = (int) (Math.pow(baiwei, ForDome01.GetLength(1000)) + Math.pow(shiwei, ForDome01.GetLength(1000)) + Math.pow(gewei, 3)); if(sum == i){ System.out.print(sum+" "); } } } public static int GetLength(int number){ //獲取輸入數的位數 int getlength = (int) Math.log10(number); return getlength; } //水仙花數(100-無限) public static int demo04(int data){ for(int i=100; i<data; i++){ } return data; } //統計水仙花數 public static void demo05(){ int count = 0; for(int i=100; i<1000; i++){ int a = i/100; int b = i/10%10; int c = i%10; int sum = (int) (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3)); if(sum == i){ count++; } } System.out.println(count); } /* * 五位數 * 個位等於萬位 * 十位等於千位 * 個位+十位+千位+萬位=百位 * */ public static void demo06(){ for(int i=10000; i<=99999; i++){ int a = i%10; //個位 int b = i/10%10; //十位 int c = i/100%10; //百位 int d = i/1000%10; //千位 int e = i/10000%10; //萬位 if((a == e) & (b == d) & (a+b+d+e==c)){ System.out.println(i); } } } /* * 統計1-1000之間滿足下列條件的數有幾個 * 對3整除餘2 * 對5整除餘3 * 對7整除餘2 */ public static void demo07(){ int count = 0; for(int i=1; i<=1000; i++){ if((i%3==2) && (i%5==3) && (i%7==2)){ count++; } } System.out.println(count); } public static void main(String[] args) { // System.out.println(ForDome01.shuchu()); // System.out.println(ForDome01.jiecheng(5)); // System.out.println(ForDome01.demo01(5)); // System.out.println(ForDome01.demo02(2)); // demo03(); // demo05(); // demo06(); demo07(); } }

while

package com.st.basis.day04;

public class WhileDemo {
	/*
	 * while迴圈語句
	 * 格式:
	 * 初始化語句;
	 * while(判斷語句){
	 * 	迴圈體語句;
	 * 	控制條件語句;
	 * }
	 * */
	public static void main(String[] args) {
//		demo01();
//		demo02();
//		demo03();
//		demo04();
		demo05();
	}
	
	//迴圈輸出helloworld
	static void demo01(){
		int i = 0;
		while(i<10){
			System.out.println("helloword");
			i++;
		}
	}
	
	//輸出1-100的和
	static void demo02(){
		int sum = 0;
		int i = 0;
		while(i<=100){
			sum += i ;
			i++;
		}
		System.out.println(sum);
	}
	
	//迴圈輸出100-1000的水仙花數
	static void demo03(){
		int i = 100;
		while(i<=1000){
			int a = i/100;
			int b = i/10%10;
			int c = i%10;	
			int sum = (int) (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3)); 
			if(sum == i){
				System.out.println(i);
			}
			i++;
		}
	}
	
	//統計水仙花數
	static void demo04(){
		int i = 100;
		int count = 0;
		while(i<1000){
			int a = i/100;
			int b = i/10%10;
			int c = i%10;	
			int sum = (int) (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3)); 
			if(sum == i){
				count++;
			}
			i++;
		}
		System.out.println(count);
	}
	
	/*
	 * 珠穆朗瑪峰8848m
	 * 一張紙厚度為0.01m
	 * 請問疊多少次等於珠穆朗瑪峰高度
	 * */
	static void demo05(){
		int max = 884800;
		int min = 1;
		int count = 0;
		while(min < max){
			count++;
			min *=2;
		}
		System.out.println(count);
	}
}

do…while

package com.st.basis.day04;

/*
 * do..while:
 *  
 * 初始化語句;
 * do{
 * 		迴圈體語句;
 * 		控制條件語句;
 * }while(判斷條件語句);
 * 
 * */

public class DoWhileDemo {

	public static void main(String[] args) {
		demo01();
	}

	//迴圈輸出hellworld
	private static void demo01() {
		String a = "Helloworld";
		int num = 0;
		do{
			System.out.println(a);
			num++;
		}while(num<10);
	}

}