package com.sosee;
import java.util.Scanner;
import com.sosee.lvc;
public class lvc {
public boolean isLeapYear(int year) {
boolean isLeap = false;
calcLeapYear();
if (year%4!=0) {
isLeap=false;
System.out.println("该年不是闰年"+year);}
else if(year%400!=0&&year%100==0)
{
isLeap=false;
System.out.println("该年不是闰年"+year);}
if (year % 4 == 0 && year % 100 != 0) {
isLeap = true;
System.out.println("该年是闰年"+year);
} else if (year % 400 == 0) {
isLeap = true;
System.out.println("该年是闰年"+year);
}
return isLeap;
}
public void calcLeapYear() {
Scanner sc=new Scanner(System.in);
int[] year=new int[5];
for(int i=0;i<year.length;i++) {
System.out.println("请输入5个年份:");
year[i]=sc.nextInt();
}
sc.close();
}
public static void main(String[] args) {
lvc studentMange=new lvc();
studentMange.calcLeapYear();
studentMange.calcLeapYear();
}
}
你这个代码问题很多。1、单独写了一个方法calcLeapYear(),并且在isLeapYear(int year)方法里面调用了这个方法,但是你没有发现你在
计算闰年的过程中全程没有用到calcLeapYear()这个方法中输入的年份。2、主方法调用的方法是calcLeapYear(),这个方法只是用来获取
输入的年份,你要计算年份是不是闰年不是应该调用isLeapYear(int year)这个方法吗?
以下是修改过的代码
public class lvc {
public static void main(String[] args) {
lvc studentMange=new lvc();
studentMange.isLeapYear(2020);
//studentMange.calcLeapYear();
}
public boolean isLeapYear(int year) {
boolean isLeap = false;
calcLeapYear();
if (year%4!=0) {
isLeap=false;
System.out.println("该年不是闰年"+year);
}
else if(year%400!=0&&year%100==0){
isLeap=false;
System.out.println("该年不是闰年"+year);
}
if (year % 4 == 0 && year % 100 != 0) {
isLeap = true;
System.out.println("该年是闰年"+year);
} else if (year % 400 == 0) {
isLeap = true;
System.out.println("该年是闰年"+year);
}
return isLeap;
}
public void calcLeapYear() {
Scanner sc=new Scanner(System.in);
int[] year=new int[5];
for(int i=0;i<year.length;i++) {
System.out.println("请输入"+(5-i)+"个年份:");
year[i]=sc.nextInt();
}
sc.close();
}
}
isLeapYear(int year)这个方法没看见被调用呢
你这个要干嘛我都不知道 你要判断的话就调用isLeapYear ,你要遍历你输入的5个年份那就遍历传参不就行了。。
试试调用isLeapYear