Java Novice Note 6

Source: Internet
Author: User

1. Calculate the number of days

/* Given one month day, the calculation is the first day of the year (e.g. input: 2 15 Result: 46th day) */public class Demo1 {public static void main (string[] args) {int year = 2012;int Mont H = 12;int Day = 31;int total = 0;//cumulative days/*switch (month-1) {//0-11case 0:total = day;break;case 1:total = + day;br Eak;case 2:total = + + Day;break;case 3:total = + + + + Day;break;}  Switch (month-1) {//0-11case 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;} *///array 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;//judgment Leap Year if (year% = = 0 | | year% 4 = = 0 && year%! = 0) {if (Month > 2)    total++;} SYSTEM.OUT.PRINTLN ("total =" + total);}}

2. Monkeys Eat Peach

/* Monkey eat peach problem. The monkey picked a few peaches on the first day, ate half of it immediately, and ate one more. The next morning he ate half of the remaining peaches and ate one more. Every morning after eating the rest of the day half of the zero one. By the 10th morning when I wanted to eat again, there was only one peach left. The first day to pick how many peach length of 10 int array, put 10 random number, output maximum, minimum number */public class Demo2 {public static void main (string[] args) {/*int total = 1;  2  -1for (int i = ten; i > 1; i--) {   total = (all + 1) * 2;//The number of peaches the day before}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. Printing

/*6   1   2   3   4 6 1 2 3 5   6   1 2   +   4   5   6   1   3   4   5 6 each   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 the index of the array back one position}}}

Java Novice Note 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.