java新手笔记6 示例for

1.计算天数

/*给定一个年月日,计算是一年的第几天

(如输入:2 15 结果:第46天)



*/

public class Demo1 {



	public static void main(String[] args){

		int year = 2012;

		int month = 12;

		int day = 31;

		int total = 0;

		//累计天数

		/*

		switch(month - 1) {//0 - 11

			case 0: total = day;break;

			case 1: total = 31 + day;break;

			case 2: total = 31 + 28 + day;break;

			case 3: total = 31 + 28 + 31 + day;break;



		}

		

		switch(month - 1) {//0 - 11

			case 11: total += 30;

			case 10: total += 31;

			case 9: total += 30;

			case 8: total += 31;

			case 7: total += 31;

			case 6: total += 30;

			case 5: total += 31;

			case 4: total += 30;

			case 3: total += 31;

			case 2: total += 28;

			case 1: total += 31;

			case 0: total += day;

		}

		*/

		//数组

		int[] a = {0,31,28,31,30,31,30,31,31,30,31,30,31};

		for(int i = 0; i < month; i++ ){

              total += a[i];

		}

		total += day;



		//判断闰年

		if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0){

			if(month > 2)

			    total++;

		}

		System.out.println("total = " + total);

	}

}

 2.猴子吃桃

/*

猴子吃桃问题。猴子第一天摘下若干个桃子,

当即吃了一半,还不过瘾,又多吃了一个。

第二天早上又将剩下的桃子吃掉了一半,又多吃了一个。

以后每天早上都吃了前一天剩下的一半零一个。

到第10天早上想再吃时,就只剩一个桃子了。

求第一天共摘多少桃子



长度为10的int数组,放入10个随机数,输出最大、最小数

*/

public class Demo2 {



	public static void main(String[] args){

		/*

		int total = 1;

        // 10  -  2    10  - 1

		for(int i = 10 ; i > 1; i-- )

		{

		   total = (total + 1) * 2;//前一天的桃子数

		}



		System.out.println("total = " + total);

		*/

		int[] a = {5,2,4,6,9,0,3,11,7,8};

		int max, min;

		max = a[0];

        min = a[0];

		for(int i = 1; i < a.length; i++) {

            if(max < a[i]) {

                max = a[i];

				System.out.println("max ===> " + max);

			}

			if(min > a[i]){

				min = a[i];

			}



		}

		System.out.println("max = " + max);

		System.out.println("min = " + min);

	}

}

 3.打印

/*

6   1   2   3   4   5

5   6   1   2   3   4

4   5   6   1   2   3

3   4   5   6   1   2

2   3   4   5   6   1

1   2   3   4   5   6



*/

public class Demo3 {



	public static void main(String[] args){

		

		int[] a = {1,2,3,4,5,6};

		int k = 5;

		for(int i = 0; i < a.length; i++) {

           for(int j = 0; j < a.length; j++) {

               System.out.print(a[k] + "  ");

			   k++;

			   if( k == 6) {

                   k = 0;

			   }



		   }

           System.out.println();

		   k--;//a数组的索引退一个位置

		}

		

		

	}

}

 

你可能感兴趣的:(java)