第六章 上課內容

import java.util.Scanner;

public class Ch06 {
	public static void main(String[] args) {
		int i=0;
		while(i<100){
			System.out.println("第"+(i+1)+"遍好好學習天天向上");
			i++;
		}
//		
		int j=0;
		for(;j<100;){
			System.out.println("第"+(j+1)+"遍好好學習天天向上");
			j++;
		}
		
		// for(表達式1;表達式2;表達式3){ // 循環體}
		/*表達式1 
		 * 	在循環初始化的時候執行
		 * 表達式2
		 * 	循環條件先判斷後執行(類似while循環)
		 * 表達式3
		 * 	每次循環結束的時候執行
		 */
		for(int k=0; k<100;k++)
		{
			System.out.println("使用for循環第"+(k+1)+"遍好好學習天天向上");
		}
		/*
		 * 學生姓名
		 * 科目成績
		 * 多少科目
		 * 總成績
		 * 算出平均成績
		 */
		String name="";
		int score=0, count=5, sum=0;
		double avg=0;
		
		Scanner input=new Scanner(System.in);
//		System.out.println("請輸入姓名");
//		name=input.next();
//		for (int m = 0; m < count; m++) {
//			System.out.println("請輸入第"+(m+1)+"成績");
//			score=input.nextInt();
//			sum+=score;// sum=sum+score;
//			
//		}
//		avg=sum/count;
//		System.out.println(name+"平均成績是"+avg);
		// 
		/*定義一個變量
		 * 	數據類型 		變量名=值;
		 * 	a	b 	c 都是int類型
		 * 	int a=0;
		 * int b=0;
		 * int c=0;
	 	
		 * 定義多個相同類型變量
		 * 	數據類型 	變量=值,	變量,......;
		 */
		int a=0,b=0,c=0;
		System.out.println("請輸入一個值");
		int val=input.nextInt();
		// 第一個表達式意思
			// 聲明兩個類型相同的變量,給k賦值爲0 給m賦值爲val
		boolean c1=true;
		for(int k=0,m=val;k<=val&&c1;k++,m--){
			System.out.println(k+"+"+m+"="+(k+m));
		}
	}
}
import java.util.Scanner;

public class CH06_1 {
	public static void main(String[] args) {
		/*
		 * 30歲以下
		 * 年齡
		 * 總人數
		 */
	 
		Scanner input=new Scanner(System.in);
		int young=0;
		int age;
		int count=10;
		for (int i = 0; i < count; i++) {
			System.out.println("請輸入第"+(i+1)+"位顧客的年齡");
			age=input.nextInt();
			if(age<=30&&age>0){
				young++;
			}
		}
		
		System.out.println("30歲以下的 "+young*1.0/count*100+"%");
		System.out.println("30歲以上的 "+(100-young*100/count)+"%");
		
	}
}

for 循環 continue        break

package cn.jbit.test;

public class ch06_3 {
	public static void main(String[] args) {
		/*
		 * 1--10 遇到4 跟7 跳過
		 * 4 是我不想看的
		 * 7是我想要的
		 */
		for (int i = 0; i <10; i++) {
			if(i==4)
			{
				continue;  // 結束本次循環(看到不想看的東西跳過,看別的)
				// 完事之後 ,執行一個i++ 第三個表達式
			}
			if(i==7){
				break;		// 跳出循環(買到我想要的東西,去結算)
				// 循環結束
			}
			System.out.println(i);
		}
		System.out.println("循環結束");
		
		
		
	}
}


發佈了43 篇原創文章 · 獲贊 6 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章