Java循環



1、順序結構


2、選擇結構


//import java.util.Scanner;

public class ifelse {
	
	public static void main(String [] args){
	
//		Scanner input = new Scanner(System.in);
	
		/**
		System.out.println("請輸入成績:");
		 
		int score = input.nextInt();
		
		if(score>=90){
			
			System.out.print("A級");
			
		}
		else if(score>=80){
			
			System.out.print("B級");
			
		}
		
		else {
			
			System.out.print("C級");
			
		}
		
		//運動會
		 * System.out.println();
		double time = input.nextInt();
		
		String gender = input.next();//=null;
		
		if(time<10){
			
			if(gender=="男"){//("男".equals(gender))
				
				System.out.print("");
				
			}else if(gender.equals("")){
				
				System.out.print("");
				
			}

		}else{
			System.out.print("淘汰");
		}
		
		//根據用戶輸入星期、氣溫、天氣進行判斷
		
		System.out.print("請輸入今天星期幾:");
		
		int week = input.nextInt();
		
		if(0<week&week<=7){
			if(week==6||week==7){
			System.out.print("請輸入今天的溫度:");
			double temperature = input.nextInt();
		    if(temperature>=30){
		   
				System.out.print("今天去游泳。");
				
			}else{
				
				System.out.print("今天去爬山。");
				
			}
			
		}else{
			System.out.print("請輸入今天的天氣:");
			String day = input.next();
			
			if(day.equals("晴")){
				
				System.out.print("今天談業務。");
				
			}else{
				System.out.print("今天上網查資料。");
			}
		}
			
		}else{
			System.out.print("一週只有七天,請輸入1-7之間的數字。");
		}
					
		//switch 判斷
		int score = input.nextInt();
		switch(score/10){
		
			case 10:
				System.out.print("A級");
			break;
		    default:
		    	System.out.print("E級");
		}
		
		
		//根據用戶輸入年份、月份進行判斷
		System.out.print("請輸入年份:");
		int year = input.nextInt();
		System.out.print("請輸入月份:");
		int month = input.nextInt();
		switch(month){
			case 2:
				if(year%4==0&year%100!=0&year%400==0){
				System.out.print("28days");
				}else{
				System.out.print("29days");
				}break;
			case 4:
			case 6:
			case 9:
			case 11:
				System.out.print("30days");
				break;
			default:
				System.out.print("31days");
		}
		
		//100求和
		int i = 1;
		int sum = 0 ;
		while(i<=100){
			sum = sum + i ;
			i++;
		}
		System.out.println("sum is :"+sum);
	//**/
		/*
		int i=3;
				
		while(i>=0){
			System.out.println("請輸入用戶名:");
			String user = input.next();
			System.out.println("請輸入密碼:");
			int password = input.nextInt();
			if("zhxj".equals(user)&&password==123456){
				System.out.print("歡迎進入系統!");
				break;
			}else{
				System.out.print("輸入錯誤,您還有"+i+"次機會!");
			}
			i--;
		}*/
		//System.out.print("請輸入0-9之間的數字:");
		/*System.out.print("請輸入一個隨機數字:");
		int result = (int)(Math.random()*10);
		int num = input.nextInt();
		while(result!=num){
				
			if(result<num){
				System.out.println("不好意思答錯了!");
				System.out.println("您猜大了!");
			}else if(result>num){
				System.out.println("不好意思答錯了!");
				System.out.println("您猜小了!");
			}
			System.out.println("請重新輸入一個隨機數字:");	
			num = input.nextInt();
				
			}
				System.out.println("恭喜您答對了!");
		
	/*
	 * public static void main(String[] args) {
	
		//System.out.print("請輸入0-9之間的數字:");
		//Scanner input = new Scanner(System.in);
		int i = 1;
		int sum =0 ;
		do{
			sum = sum + i;
			i++;
		}while(i <= 100);
		System.out.print("Sum is:" + sum);
	}
	 */
	
	/*
	 * int i;
		
		do{
			System.out.println("************歡迎光臨QQ登陸頁面***********");
			System.out.println("1、註冊");
			System.out.println("2、登錄");
			System.out.println("3、退出");
			System.out.println("您的輸入是:");
			i = input.nextInt();
			
		}while(i!=3);
		
		System.out.println("************再見!***********");
		
		
		int sum = 0;
		for(int i = 1;i <= 100;i ++){
			
			sum += i;
		}
		System.out.println("SUM=" + sum);
	 */
				
	/*System.out.print("請輸入學生的姓名:");
		
		String name = input.next();
		int sum = 0;	
		for(int i = 1; i<=5;i++){
			System.out.println("請輸入第"+i+"門的成績:");
			int score = input.nextInt();
			
			sum = sum + score;
		}
			System.out.println(name+"的平均分是:"+sum/5);
			
			
				String y ;
				do{
					System.out.print("請輸入學生的姓名:");
					String name = input.next();
					int sum = 0;
					for(int i = 1; i<=5;i++){
						System.out.println("請輸入5門功課中第"+i+"門的成績:");
						int score = input.nextInt();
						sum = sum + score;
					}
						System.out.println(name+"的平均分是:"+sum/5);
						System.out.print("繼續輸入嗎?(y/n)");
						y = input.next();
										
				}while("y".equals(y));
				
				System.out.print("成績錄入結束!");		
				
				
				
				for(int i=1;i<=10;i++){
			
			if(i % 4 == 0){
				break;
				}
			System.out.print(i);
		}
		System.out.print("循環結束。");
				*/
		//百元百雞
		for(int n=0;n<100/5;n++){//公雞購買數量;
			
	       for(int m=0;m<100/3;m++){//母雞購買數量;
			
		    	int l = 100-n-m;//購買小雞的數量;
		    		
		    		if((n*5+m*3+l/3)==100&&(l%3==0)){
		    			System.out.println(n+","+m+","+l);	
		   		    			
		    	}
		    } 
		}
	}		
}



3、循環結構

for循環

練習

百元百雞

公雞一隻5元,母雞一隻3元,小雞三隻1元,一百元買一百隻雞,問各多少隻

提示

計算公雞、母雞的取值範圍(最多分別能買多少隻)

通過公雞、母雞判斷小雞的取值範圍

根據條件(公雞*5+母雞*3+小雞/3.0=100)進行計算

public class ifelse {
	
	public static void main(String [] args){
	
	for(int n=0;n<100/5;n++){ //公雞購買數量;
			
	       for(int m=0;m<100/3;m++){ //母雞購買數量;
			
		    	int l = 100-n-m; //購買小雞的數量;
		    		
		    		if((n*5+m*3+l/3)==100&&(l%3==0)){
		    		
		    			System.out.println(n+","+m+","+l);	
		   		    			
		    	}
		    } 
		}
	}		
}

結果驗證:

0,25,75

4,18,78

8,11,81

12,4,84


示例2:

//求3-100中的所有素數:素數只能被1和自己整除的數字

public class sum {

	public static void main(String [] args){
	
		//Scanner input = new Scanner(System.in);
		
		//求3-100中的所有素數:素數只能被1和自己整除的數字
		
		for(int b=2;b<100;b++){
		
			for(int a=2;a<=b;a++){
			
				if(b%a==0){
				
					if(a==b){
					
						System.out.print(b+" ");
					}
					
					break;
				}
			}
		}
	}
}

結果驗證:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 



傳參示例:

public class calc {

	public void calc1(int num){
		
		 num = num + 1;
		
	}
	
	public void calc2(students stu){
		
		stu.setAge(stu.getAge()+1);
		
	}
	
	
}	


public class calc {

	public void calc1(int num){
		
		 num = num + 1;
		
	}
	
	public void calc2(students stu){
		
		stu.setAge(stu.getAge()+1);
		
	}
	
	
}	

對象數組:


	
	public class students{
		//students類的屬性
		private String name;
		
		private int age;
		
		private String gender;
		//students類的構造方法
		public students(){
			//空方法,默認值
		}
		public students(String name,int age,String gender){
			//students類的構造方法
			this.name = name ;
			this.age = age ;
			this.gender = gender ;
			
		}
		//students的get方法獲取屬性值
		public String getName(){
			
			return this.name;
			
		}
		
		public int getAge(){
						
			return this.age;
			
		}
		
		public String getGender(){
			
			return this.gender;
			
		}
		//students的set方法對屬性進行賦值
		public void setName(String name){
			
			this.name = name ;
		}
		public void setAge(int age){
		//對age進行範圍圈定和判斷
			if(age>45 || age<15){
				
				this.age=18;
				
			}else{
				
				this.age = age ;
			}
			
		}
		public void setGender(String gender){
			
			this.gender = gender ;
		}
	}	
	


public class studentsDemo {

	public static void main(String[] args) {
		//實例化對象
		students st = new students() ;
						
//		st.name="小明";
//		st.age=12;
//		st.gender="男";
		
//		System.out.println(st.name+"\n"+st.age+"\n"+st.gender+"\n");
		
	//	st.setName("小虎");
	//	st.setAge(14);
	//	st.setGender("男");
		
		
	//	System.out.println(st.getName()+"\n"+st.getAge()+"\n"+st.getGender()+"\n");
		//1、定義對象數組
		students[] arrs = new students [3];
		//2、實例化對象
		students s1 = new students("張三",15,"男");
		students s2 = new students("李四",20,"女");
		students s3 = new students("王五",31,"男");
		//3、將對象放入對象數組
		arrs[0] = s1;
		arrs[1] = s2;
		arrs[2] = s3;
		//students[]arrs={s1,s2,s3};
		//students[]arrs={new students("張三",15,"男"),
		                //new students("李四",20,"女"),
	                 	//new students("王五",31,"男")};
		//4、遍歷輸出對象數組裏的對象
		for(students i : arrs){
			
			System.out.println(i.getName()+"\t"+i.getAge()+"\t"+i.getGender()+"\t");
		}
			System.out.println("\n");
		for(int i = 0; i < arrs.length; i++){
			
			System.out.println(arrs[i].getName()+"\t"+arrs[i].getAge()+"\t"+arrs[i].getGender()+"\t");
		}
	}

}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章