-2

LeapYear メソッドが LeapYear であるかどうかを返すようにしようとしています (実際の if ステートメントを返します)。基本的に、私はコーディングが初めてで、int や double ではなく文字列値を返す方法がわかりません。誰でもこれで私を助けることができますか?

public static int LeapYear(int y) {

    int theYear;
    theYear = y;

    if (theYear < 100) {
        if (theYear > 40) {
            theYear = theYear + 1900;
        } else {
            theYear = theYear + 2000;
        }
    }

    if (theYear % 4 == 0) {
        if (theYear % 100 != 0) {
            System.out.println("IT IS A LEAP YEAR");
        } else if (theYear % 400 == 0) {
            System.out.println("IT IS A LEAP YEAR");
        } else {
            System.out.println("IT IS NOT A LEAP YEAR");
        }
    } else {
        System.out.println("IT IS NOT A LEAP YEAR");
    }
}
4

3 に答える 3

4

intやdoubleではなく文字列値を返す方法がわかりません。

リターンタイプをString

public static String leapYear(int y)

そして、intの代わりにStringを返します

return "IT IS NOT A LEAP YEAR";
于 2012-10-12T11:07:15.597 に答える
1

次の方法を使用できます。

static boolean isLeapYear(final int year) {
    return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

そう:

public static void LeapYear(int y) {
    if (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {
        System.out.println("IT IS A LEAP YEAR");
    } else {
        System.out.println("IT IS NOT A LEAP YEAR");
    }
}
于 2014-03-28T21:40:42.320 に答える
1
public static String LeapYear(int y) {
 int theYear;
 theYear = y;
 String LEAP_YEAR = "IT IS A LEAP YEAR";
 String NOT_A_LEAP_YEAR = "IT IS NOT A LEAP YEAR";

 if (theYear < 100) {
    if (theYear > 40) {
        theYear = theYear + 1900;
    } else {
        theYear = theYear + 2000;
    }
 }

if (theYear % 4 == 0) {
    if (theYear % 100 != 0) {
        //System.out.println("IT IS A LEAP YEAR");
        return LEAP_YEAR;

    } else if (theYear % 400 == 0) {
        //System.out.println("IT IS A LEAP YEAR");
        return LEAP_YEAR;
    } else {
       // System.out.println("IT IS NOT A LEAP YEAR");
       return NOT_A_LEAP_YEAR ;
    }
  } else {
    //System.out.println("IT IS NOT A LEAP YEAR");
    return NOT_A_LEAP_YEAR ;
  }
 return NOT_A_LEAP_YEAR ;
}
于 2012-10-12T11:14:36.667 に答える