java 年计算_用Java计算leap年
這是家庭作業,我已經獲得了成績,但是我沒有在代碼中實現leap年。這是一個簡單的程序,可以根據用戶輸入顯示一個月內的數字。我唯一不知道的是一種實現implement年的方法,這種writing年將在2月獲得29天而不是28天,而無需編寫多條if語句。當然有更簡單的方法嗎?這是代碼:
//Displays number of days in a month
package chapter_3;
import java.util.Scanner;
public class Chapter_3 {
public static void main(String[] args) {
System.out.println("This program will calculate \n"
+ "the number of days in a month based upon \n"
+ "your input, when prompted please enter \n"
+ "the numerical value of the month you would like\n"
+ "to view including the year. Example: if you would like\n"
+ "to view March enter 3 2011 and so on.");
Scanner input = new Scanner(System.in);
//Prompt user for month and year
System.out.print("Please enter the month and year: ");
int month = input.nextInt();
int year = input.nextInt();
if (month == 1) {
System.out.println("January " + year + " has 31 days");
}
else if (month == 2) {
System.out.println("February " + year + " has 28 days");
}
else if (month == 3) {
System.out.println("March " + year + " has 31 days");
}
else if (month == 4) {
System.out.println("April " + year + " has 30 days");
}
else if (month == 5) {
System.out.println("May " + year + " has 31 days");
}
else if (month == 6) {
System.out.println("June " + year + " has 30 days");
}
else if (month == 7) {
System.out.println("July " + year + " has 31 days");
}
else if (month == 8) {
System.out.println("August " + year + " has 31 days");
}
else if (month == 9) {
System.out.println("September " + year + " has 30 days");
}
else if (month == 10) {
System.out.println("October " + year + " has 30 days");
}
else if (month == 11) {
System.out.println("November " + year + " has 30 days");
}
else if (month == 12) {
System.out.println("December " + year + " has 31 days");
}
else {
System.out.println("Please enter the numerical value "
+ "of the month you would like to view");
}
}
}
總結
以上是生活随笔為你收集整理的java 年计算_用Java计算leap年的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 字典的值可以为集合吗_转:
- 下一篇: java 向量空间模型_VSM向量空间模