簡體   English   中英

對於循環:Le年

[英]For Loop: Leap year

我正在嘗試編寫一個顯示每行十個line年的程序,直到2100年。

到目前為止,我的代碼是:

public class leapYear {
public static void main (String args[]){
    int leapYear = 2001; // initialize leapYear variable to 2001

    if (leapYear <= 2100){
        for (int x = 0; x <= 10; x++){ // print ten leap years per line
            System.out.print(leapYear + " ");
            leapYear = leapYear + 4;
        }// end of for loop
        System.out.println(" ");
        int x = 0;
    } // end of if statement

}// end of main method

} //課程結束

我從代碼中得到的輸出是:

2001 2005 2009 2013 2017 2021 2025 2029 2033 2037 2041  

我在尋找一種條件時遇到了麻煩,該條件使我可以在以下幾行上打印下一個系列的Y年。 任何指導將不勝感激。

if(leapYear <= 2100)行更改為while(leapYear <= 2100)

if語句僅執行一次,這意味着您將打印10個日期。 while循環將執行的多次次數,leap年大於2100所需的時間。

您當前正在使用if語句來檢查是否要打印某些內容( if (leapYear <= 2100) )。 但是,在for-loop之后,您想再次檢查在到達2100年之前是否還有更多leap年要打印。 這可以通過使用while循環來實現,即while(leapYear < 2100) (由於2100年不是a年,請參見下文)

此外,應該指出,2004年是leap年的更好起點,而2100年不是a年(2000年是)。 有關更多信息,請參見Wiki

與問題無關的第二個注釋,您也可以使用System.out.println(); (因此不帶參數)以打印您要的換行符。

第三行也是最后一行,行int x = 0; 不需要,因為for循環中的計數器x不再存在於該循環之外。

如果If語句給您帶來麻煩,它將僅執行一次。 您需要類似while循環的內容,也可以使用for循環,

                for (int leapYear = 2001; leapYear <= 2100; leapYear += 4) {
                    if((leapYear - 1) % 40 == 0) {
                        System.out.println(" ");
                    }
                    System.out.print(leapYear + " ");
                }

但是,我想指出的是,您打印出的年份都不是leap年。 閏年是那些被4整除,除了由100整除時除外時也被400整除(即2000年是閏年2004年和2100不會是,但2400會)超級混亂,你可能要檢查這個出來閏年。

祝好運!

您問如何每行打印10個leap年。 為此,只需更改:
if (leapYear <= 2100) {
至:
while (leapYear <= 2100) {

這里有完整的工作和經過測試的示例:

public class LeapYear {
    public static void main(String[] args) {

        int leapYear = 2001; // initialize leapYear variable to 2001

        while (leapYear <= 2100) { // a simple change of "If" to "While" here
                                    // will do the trick
            for (int x = 0; x < 10; x++) {
                System.out.print(leapYear + " "); // print ten leap years per
                                                    // line
                leapYear = leapYear + 4;
            }
            System.out.println();
        } // end of while loop
    } // end of main method
}// end of class

話雖如此,我將推薦一些已經提到的事情。

1-您的代碼中存在一些邏輯錯誤,這些錯誤將錯誤地命名為幾年leap年。 有關Le年的更多信息,請參見此處

2-如果您嘗試我的示例,您還將注意到that年顯示在2100年以后。如果您不希望數字顯示在2100年以后,則需要找出一種正確構造代碼的方法。

3-我強烈建議您看一下GregorianCalendar類,因為它將簡化您的代碼以檢查a年。 但是,如果您這樣做是為了學習或進行學校作業,建議您查看編號1下的鏈接。

4-還有一點,但是在您的代碼中,您有:

leapYear = leapYear + 4

可以簡化為:

leapYear += 4

希望這可以幫助您澄清問題。

為leap年提供正確的實施:

public void main(String[] args) {
    for(int year = 2001 ; year <= 2100 ; ++year) {
        if(isLeapYear(year)) {
            System.out.println(year);
        }
    }
}

public static boolean isLeapYear(int year) {
    if (year % 4 != 0) {
        return false;
    } else if (year % 400 == 0) {
        return true;
    } else if (year % 100 == 0) {
        return false;
    } else {
        return true;
    }
}
public static void main (String args[]){
    int leapYear = 2001; // initialize leapYear variable to 2001

    while (leapYear <= 2100){
        for (int x = 0; x < 10; x++){ // print ten leap years per line
            if (leapYear > 2100){ // if leapYear is over 2100, stop printing
                break;
            }
            System.out.print(leapYear + " ");
            leapYear = leapYear + 4;
        }// end of for loop
        System.out.println(" ");
        int x = 0;
    } // end of if statement

}// end of main method

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM