Java Fundamentals 04

Source: Internet
Author: User

Today to see a few exercises on the date and year, which contains the while infinite loop, if the judgment is not correct, the user will always be stuck in the loop, until the user input is correct, these exercises are very logical, now let's see how these exercises are done.

1. Let the user enter the day of the month, output this is the day of the year

Import Java.util.Scanner;

public class Problem13 {

public static void Main (string[] args) {

Scanner scan = new Scanner (system.in);

int year, month, day;

while (true) {

System.out.print ("Please enter the year (1900-2200):");

Year = Scan.nextint ();

if (Year < 1900 | |, Year > 2200) {

SYSTEM.OUT.PRINTLN ("You entered the wrong year, please re-enter");

} else {

Break

}

}

while (true) {

System.out.print ("Please enter the month (1-12):");

month = Scan.nextint ();

if (Month < 1 | | month > 12) {

SYSTEM.OUT.PRINTLN ("You entered the wrong month, please re-enter");

} else {

Break

}

}

while (true) {

System.out.print ("Please enter Number:");

Day = Scan.nextint ();

if (month==2) {

if (year%4==0&&year0!=0| | [email protected]==0] {

if (day>=1&&day<=29) {

Break

}

}else{

if (day>=1&&day<=28) {

Break

}

}

}else if (month<=7&&month%2!=0| | month>7&&month%2==0) {

if (day>=1&&day<=31) {

Break

}

}else{

if (day>=1&&day<=30) {

Break

}

}

SYSTEM.OUT.PRINTLN ("You entered the wrong, please re-enter");

}

Mode 1

int sum=0;

Switch (month) {

Case 1:

sum=0;

Break

Case 2:

sum=31;

Break

Case 3:

sum=59;

Break

Case 4:

sum=90;

Break

Case 5:

sum=120;

Break

Case 6:

sum=151;

Break

Case 7:

sum=181;

Break

Case 8:

sum=212;

Break

Case 9:

sum=243;

Break

Case 10:

sum=273;

Break

Case 11:

sum=304;

Break

Case 12:

sum=334;

Break

Default:

Break

// }

Sum+=day;

Judge a leap year or common year

int add;

if ((year%4==0&&year0!=0) | | | [email protected]==0] {

add=1;

}else{

Add=0;

// }

//

if (add==1&&month>2) {

sum++;

// }

//

System.out.println ("This is the" +sum+ "Day of the +year+" year ");

Mode 2

int sum = 0;

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

The number of days per month is different

if (i = = 2) {//Leap year for peace years judgment

if ((i%4==0&&i0!=0) | | | [email protected]==0] {

Leap year

sum+=29;

}else{

sum+=28;

} or

sum + = 28;

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

sum++;

}

} else if (I <= 7 && i% 2! = 0 | | i > 7 && i% 2 = 0) {//less than or equal to July odd month is Otsuki, greater than July even month is Otsuki

sum + = 31;

} else {//other month is Yue

sum + = 30;

}

}

Enter Day

sum + = day;

System.out.println ("This is" + year + "Years of the first" + Sum + "days");

}

}

2. Let the user enter the year, the output from January 1, 1900, to the date of the month of 1st through the number of days

Import Java.util.Scanner;

public class Problem14 {

public static void Main (string[] args) {

Scanner scan=new Scanner (system.in);

int year, month;

while (true) {

System.out.print ("Please enter the year (1900-2200):");

Year = Scan.nextint ();

if (Year < 1900 | |, Year > 2200) {

SYSTEM.OUT.PRINTLN ("You entered the wrong year, please re-enter");

} else {

Break

}

}

while (true) {

System.out.print ("Please enter the month (1-12):");

month = Scan.nextint ();

if (Month < 1 | | month > 12) {

SYSTEM.OUT.PRINTLN ("You entered the wrong month, please re-enter");

} else {

Break

}

}

int Day=1;

int sum=0;

How many days in the first one months of the month will we receive the sum?

Switch (month) {

Case 1:

sum=0;

Break

Case 2:

sum=31;

Break

Case 3:

sum=59;

Break

Case 4:

sum=90;

Break

Case 5:

sum=120;

Break

Case 6:

sum=151;

Break

Case 7:

sum=181;

Break

Case 8:

sum=212;

Break

Case 9:

sum=243;

Break

Case 10:

sum=273;

Break

Case 11:

sum=304;

Break

Case 12:

sum=334;

Break

Default:

Break

//   }

//

//

//

Enter the year to determine whether it is leap years or common year

int add=0;

//

for (int i=1900;i<=year;i++) {

if ((i%4==0&&i0!=0) | | | [email protected]==0] {

add=1;

}else{

Add=0;

//    }

//

if (add==1&&month>2) {

sum++;

//    }

//

//

//  }

//

sum=365* (year-1900) +sum;

SYSTEM.OUT.PRINTLN (sum);

Method 2

int sum=0;

for (int i=1900;i

if ((i%4==0&&i0!=0) | | | [email protected]==0] {

sum++;

}

sum+=365;

}

for (int j=1;j

The number of days per month is different

if (j==2) {//Leap year and peace years judgment

if ((i%4==0&&i0!=0) | | | [email protected]==0] {

Leap year

sum+=29;

}else{

sum+=28;

} or

sum+=28;

if ((year%4==0&&year0!=0) | | | [email protected]==0] {

sum++;

}

}else if (j<=7&&j%2!=0| | j>7&&j%2==0) {//is less than or equal to July odd months is Otsuki, greater than July and even months is Otsuki

sum+=31;

}else{//other months is Xiao Yue

sum+=30;

}

}

SYSTEM.OUT.PRINTLN (sum);

}

}

3. Let the user enter the month, the output of the year 1th is the day of the week (known January 1, 1900 is Monday)

Import Java.util.Scanner;

public class Problem15 {

public static void Main (string[] args) {

Scanner scan=new Scanner (system.in);

int year, month, day;

while (true) {

System.out.print ("Please enter the year (1900-2200):");

Year = Scan.nextint ();

if (Year < 1900 | |, Year > 2200) {

SYSTEM.OUT.PRINTLN ("You entered the wrong year, please re-enter");

} else {

Break

}

}

while (true) {

System.out.print ("Please enter the month (1-12):");

month = Scan.nextint ();

if (Month < 1 | | month > 12) {

SYSTEM.OUT.PRINTLN ("You entered the wrong month, please re-enter");

} else {

Break

}

}

while (true) {

System.out.print ("Please enter Number:");

Day = Scan.nextint ();

if (month==2) {

if (year%4==0&&year0!=0| | [email protected]==0] {

if (day>=1&&day<=29) {

Break

}

}else{

if (day>=1&&day<=28) {

Break

}

}

}else if (month<=7&&month%2!=0| | month>7&&month%2==0) {

if (day>=1&&day<=31) {

Break

}

}else{

if (day>=1&&day<=30) {

Break

}

}

SYSTEM.OUT.PRINTLN ("You entered the wrong, please re-enter");

}

int sum=0;

for (int i=1900;i

if ((i%4==0&&i0!=0) | | | [email protected]==0] {

sum++;

}

sum+=365;

}

for (int j=1;j

The number of days per month is different

if (j==2) {//Leap year and peace years judgment

if ((i%4==0&&i0!=0) | | | [email protected]==0] {

Leap year

sum+=29;

}else{

sum+=28;

} or

sum+=28;

if ((year%4==0&&year0!=0) | | | [email protected]==0] {

sum++;

}

}else if (j<=7&&j%2!=0| | j>7&&j%2==0) {//is less than or equal to July odd months is Otsuki, greater than July and even months is Otsuki

sum+=31;

}else{//other months is Xiao Yue

sum+=30;

}

}

Sum+=day-1;

Switch (sum%7) {

Case 0:

System.out.println ("Monday");

Break

Case 1:

System.out.println ("Tuesday");

Break

Case 2:

System.out.println ("Wednesday");

Break

Case 3:

System.out.println ("Thursday");

Break

Case 4:

System.out.println ("Friday");

Break

Case 5:

System.out.println ("Saturday");

Break

Case 6:

System.out.println ("Sunday");

Break

//

//     }

System.out.println ("After" +sum+ "Days");

SYSTEM.OUT.PRINTLN (year+ "year" +month+ "month" +day+ "Day is Week" + (sum%7+1));

}

Java Fundamentals 04

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.