帮忙修改下JAVA程序

来源:百度知道 编辑:UC知道 时间:2024/06/21 03:54:24
import java.util.*;
public class DayNum {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print ("输入年份: ");
int year = input.nextInt ();
boolean LeapYear;
if ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
System.out.println(year + "年是闰年");
}else{
System.out.println(year + "年不是闰年");
}

System.out.print ("输入月份: ");
int month = input.nextInt ();
if (month < 1 || month > 12) {
System.out.println("输入的月份不正确");
if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12) {
System.out.println(year + "年" + month + "月的天数是31天");
} else if (month == 2 && (year)) {
System.out.println(year + "年" + month + "月的天数是29天");
} else if (month == 2 && (

在这里对boolean LeapYear进行赋值。如果是闰年LeapYear=true否则=false
代码如下:
if ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
LeapYear=true
System.out.println(year + "年是闰年");
}else{
LeapYear=false;
System.out.println(year + "年不是闰年");
}
然后在后面的year用LeapYear就可以了

else if (month == 2 && (LeapYear)) {
System.out.println(year + "年" + month + "月的天数是29天");
} else if (month == 2 && (!LeapYear)) {
System.out.println(year + "年" + month + "月的天数是28天");
}
注意后面应该是!LeapYear

不错

import java.util.*;
public class DayNum {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print ("输入年份: ");
int year = input.nextInt ();
boolean LeapYear;
if ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
LeapYear = true;
System.out.println(year + "年