/*
1.输入年月日,求某年(月)的总天数。
*/
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("输入年月日,空格隔开:");
ArrayList list = judge();
int year = (int) list.get(0);
int month = (int) list.get(1);
int day = (int) list.get(2);
System.out.print("请问想要求月还是年:");
String choice = choose();
boolean b =
(year % 4 == 0 & year % 400 != 0) | (year % 400 == 0);
if (b){
System.out.println("该年为闰年");
}else{
System.out.println("该年为平年");
}
if (choice.equals("年")){
if (b){
System.out.println("该年有366天");
}
else{
System.out.println("该年有365天");
}
}else{
if (month == 1 | month == 3 | month == 5 | month == 7 | month == 8 | month == 10 | month == 12){
System.out.println("该月有31天");
}else if (month == 2){
if (b){
System.out.println("该月有29天");
}else{
System.out.println("该月有28天");
}
}else{
System.out.println("该月有30天");
}
}
}
public static ArrayList judge() {
Scanner sc = new Scanner(System.in);
ArrayList list = new ArrayList();
try {
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
boolean b =
(year % 4 == 0 & year % 400 != 0) | (year % 400 == 0);
if (year < 0) {
System.out.print("输入错误!请重新输入:");
judge();
} else if (month < 1 | month > 12) {
System.out.print("输入错误!请重新输入:");
judge();
} else if (month == 1 | month == 3 | month == 5 | month == 7 | month == 8 | month == 10 | month == 12) {
if (day < 1 | day > 31) {
System.out.print("输入错误!请重新输入:");
judge();
}
} else if (month == 2) {
if (b) {
if (day < 1 | day > 29) {
System.out.print("输入错误!请重新输入:");
judge();
}
} else {
if (day < 1 | day > 28) {
System.out.print("输入错误!请重新输入:");
judge();
}
}
} else {
if (day < 1 | day > 30) {
System.out.print("输入错误!请重新输入:");
judge();
}
}
list.add(year);
list.add(month);
list.add(day);
}catch (InputMismatchException e){
System.out.print("输入错误!请重新输入:");
judge();
}
return list;
}
public static String choose(){
Scanner sc = new Scanner(System.in);
String choice = sc.next();
try {
if (!choice.equals("年") & !choice.equals("月")){
System.out.print("输入错误!请重新输入:");
choose();
}
}catch (InputMismatchException e){
System.out.print("输入错误!请重新输入:");
choose();
}
return choice;
}
}
使用java编程:输入一个年月日,求该年(月)的总天数。
最新推荐文章于 2024-07-27 13:31:57 发布