Java Process Control Exercise--perpetual calendar

Source: Internet
Author: User

Java Process Control Exercise--perpetual calendar

Tags: Java into the pit tour

0x01. Print inverted triangle and positive triangle
    public static void main(String[] args) {        // TODO Auto-generated method stub        int i;        int j;                /**         *  要求:打印一个倒三角以及一个正三角         *  方法:双层循环         **/        for(i=3;i>0;i--) {            for(j=1;j<=i;j++) {                System.out.print("*");            }            System.out.println();        }        for(i=1;i<4;i++) {            for(j=1;j<=i;j++) {                System.out.print("*");            }            System.out.println();        }    }
Output:************
0x02.99 multiplication Table 1 (Zheng Li)
    public static void main(String[] args) {        // TODO Auto-generated method stub        /**         *  要求:打印九九乘法表         *  方法:双层循环         **/        for(int i = 1;i<=9;i++) {            for(int j = 1;j<=i;j++) {                System.out.printf("%d*%d=%-2d\t",j,i,i*j);            }            System.out.println();        }    }
Output:1*1=1   1*2=2   2*2=4   1*3=3   2*3=6   3*3=9   1*4=4   2*4=8   3*4=12  4*4=16  1*5=5   2*5=10  3*5=15  4*5=20  5*5=25  1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  
0x03.99 Multiplication Table 2 (inverted)
  public static void Main (string[] args) {for (int i = 1; I <= 9;  ++i) {for (int j = 1; J <= 9; j + +) {if (J < i) {//Output space by "%2d*%2 =%-2d                "Decision System.out.print (" ");                } else {System.out.printf ("%d*%d=%-2d\t", I, J, I*j);        }} System.out.println (); }    }
Output:1*1=1   1*2=2   1*3=3   1*4=4   1*5=5   1*6=6   1*7=7   1*8=8   1*9=9           2*2=4   2*3=6   2*4=8   2*5=10  2*6=12  2*7=14  2*8=16  2*9=18                  3*3=9   3*4=12  3*5=15  3*6=18  3*7=21  3*8=24  3*9=27                          4*4=16  4*5=20  4*6=24  4*7=28  4*8=32  4*9=36                                  5*5=25  5*6=30  5*7=35  5*8=40  5*9=45                                          6*6=36  6*7=42  6*8=48  6*9=54                                                  7*7=49  7*8=56  7*9=63                                                          8*8=64  8*9=72                                                                  9*9=81                                                                
0x04. Perpetual calendar calendar4.1 Solution Ideas

Example 1: February 1900.

    1. 1900 for common year, February for 28 days.
    2. February 01 is 31 days from January 01 (30+1=31).
    3. Every 7 days is 1 weeks, (31%7=3) so February 01 is Wednesday (Date[0] for Sunday).
    4. Starting from No. 01, the loop output to 28 can be;

Example 2: April 1900.

    1. 1900 is common year, February has 28 days.
    2. April 01 is 90 days from January 01 (30+28+31+1=90).
    3. Every 7 days is 1 weeks, (90%7=6) so April 01 is Saturday (Date[0] for Sunday).
    4. Starting from No. 01, the loop output to 30 can be;

4.2 Algorithmic Thinking
    1. Confirm that the input year is a leap years. If it is a leap year, the number of days in February is 29, otherwise 28 days.
    2. The number of days to solve the input month distance from January 1, 1900;
    3. Make sure that number No. 01 of the month is the day of the week;
    4. Output from number No. 01 to the last day of the month.
4.3 Java code
Package Com.ryanjie.test0727;import Java.util.scanner;public class Calendar {/** * function: Determine if the input year is a leap years * @ PARAM year * @return */static Boolean leapyear (int year) {if (year% 400 = = 0 | | (year% 4 = = 0 && Year% 100! = 0))         {//is a leap year return true;        } else {//is common year return false;            }}//n Indicates the start year static final int N = 1900; /** * Function: First confirm the current month distance January 1, 1900 days, and then determine the month of No. 01 is the day of the week, * after the control output calendar * @param currentyear * @pa Ram Currentmonth */static void MonthCalendar (int currentyear,int currentmonth) {//Sumdays used to store a common        How many days int sumdays = 0;                 Store 12 months of days (no 0月, so month[0]=0) int month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};                        The January 01 of each year stored in 1900-11969 is the day of the week year[0] = 1 for the January 01, 1970 for the Monday for (int i = N;i < Currentyear;i + +) { Days is used to store a previous yearTotal number of days (366/365) int day = 365;                if (Leapyear (i)) {days + +;            Sumdays = Sumdays + days;            } else{sumdays = Sumdays + days;        }}//If it is a leap year, February is changed to number 29th if (leapyear (currentyear)) {month[2] = 29;        } for (int i=1;i<currentmonth;i++) {sumdays = sumdays + month[i];        }//week used to store the current month number No. 01 is the day of the week Int week;                Week = (sumdays + 1)% 7;        System.out.println ("Sunday \ T" + "Monday \ T" + "Tuesday \ T" + "Wednesday \ T" + "Thursday \ T" + "Friday \ T" + "Saturday \ t");        System.out.println ();        Output control for (int i = 0;i < week; i++) {System.out.print ("\ t");            } for (int i = 1; I <= month[currentmonth]; i + +) {System.out.printf ("%-2d \ T", I);            if ((week)% 7 = = 6) {System.out.println ();        } week + +; }} PublIC static void Main (string[] args) {while (true) {int. Month, year;            Scanner in = new Scanner (system.in);            System.out.println (); SYSTEM.OUT.PRINTLN ("******** welcome you to use the perpetual calendar!"            ********");            System.out.println ("Please enter the year <1900~11900>:");            Year = In.nextint ();                if (year < 1900) {System.out.println ("Enter years is not legal, please re-enter!");            Year = In.nextint ();            } System.out.println ("Please enter month <1~12>:");            Month = In.nextint ();  if (Month < 1 | | |                Month > {System.out.println ("Enter the month is not legal, please re-enter!");            Month = In.nextint ();            } MonthCalendar (Year,month);            System.out.println (); System.out.println ("******** Thank you for using the perpetual calendar!        ********"); }           }}
Output:********欢迎您使用万年历!********请输入年份<1900~11900>:2018请输入月份<1~12> :7星期日 星期一 星期二     星期三     星期四     星期五     星期六       1       2       3       4       5       6       7       8       9       10      11      12      13      14      15      16      17      18      19      20      21      22      23      24      25      26      27      28      29      30      31    ********感谢您使用万年历!********

Emmm ..... The output format is correct, but the markdown format will be wrong, headache ....

Java Process Control Exercise--perpetual calendar

Related Article

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.