qq_28799449 2018-12-13 09:23 采纳率: 100%
浏览 354
已采纳

java菜鸟一枚希望大神可以帮我改一下代码。代码除了问题但是没有报错。

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();

}
}

  • 写回答

3条回答 默认 最新

  • 超超侠 2018-12-13 13:58
    关注

    你这个代码问题很多。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();
    

    }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?