2016-10-15 24 views
2

首先,我對我的英文表示歉意。當談到解釋想法時,與編程相關的問題我仍然有困難要清楚哪些問題和我想要的。如何驗證布爾值是真是假

代碼:

public static boolean isLeapYearJulian(int year) { 
    // Modifier le code ci-dessous 
     if (year % 4 == 0) { 
      return true; 
     } 
     else { 
      return false; 
     } 

    } 

    public static boolean isLeapYearGregorian(int year) { 
    // Modifier le code ci-dessous 
     if ((year % 4 == 0) && (year % 100 != 0) || (year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0)) { 
      return true; 
     } 
     else { 
      return false; 
     } 

    } 

    // EXERCICE 2 QUESTION 2 
    public static int daysInYearJulian(int year) { 
    // Modifier le code ci-dessous 
     if (isLeapYearJulian == true) { 
      return 366; 
     } 
     else { 
      return 365; 
     } 
    } 

    public static int daysInYearGregorian(int year) { 
    // Modifier le code ci-dessous 
     if (isLeapYearGregorian == true) { 
      return 366; 
     } 
     else { 
      return 365; 
     } 
    }` 

的事情是,我想看看是否isLeapYearGregorian和isLearYearJulian是真的還是不知道,如果今年是bisextile。但是(是的,我是新的,編程非常新)我只是不記得測試一個布爾值......所以有一個很遺憾的是,我向你們求助......在此先感謝。

順便提一下,終端返回此:

Calendar.java:47: error: cannot find symbol 
     if (isLeapYearJulian == true) { 
      ^
    symbol: variable isLeapYearJulian 
    location: class Calendar 
Calendar.java:57: error: cannot find symbol 
     if (isLeapYearGregorian == true) { 
      ^
    symbol: variable isLeapYearGregorian 
    location: class Calendar 
2 errors 
+0

關於您的問題標題:如何驗證布爾值是true還是false - 使用if塊。 –

+1

您正在調用方法而不使用括號或傳入參數。編譯器錯誤信息會告訴你在哪裏。投票結束作爲印刷錯誤。 –

+0

@TierOne隨意標記[我的答案](http://stackoverflow.com/questions/40064006/how-to-verify-if-a-boolean-is-true-or-false/40064059#40064059)爲接受(綠色標記),如果它解決了你的問題:) –

回答

2

更換

if (isLeapYearJulian == true) 

if (isLeapYearJulian(age)) 
+0

問題解決了,非常感謝你的快速和有用的答案!再次感謝 ! – TierOne

+0

@TierOne沒有問題,考慮標記這個答案或另一個如果它的作品被接受;) –

+0

並歡迎您的方式 –

2

我想你想這

public static int daysInYearGregorian(int year) { 
    boolean isLeapYearGregorian = isLeapYearGregorian(year); 
    if (isLeapYearGregorian) { 
     return 366; 
    } 
    else { 
     return 365; 
    } 
}` 

或者更簡單地說

public static int daysInYearGregorian(int year) { 
    if (isLeapYearGregorian(year)) { 
     return 366; 
    } 
    else { 
     return 365; 
    } 
}` 

,甚至更簡單

public static int daysInYearGregorian(int year) { 
    return isLeapYearGregorian(year) ? 366 : 365; 
}` 
1
// EXERCICE 2 QUESTION 2 
public static int daysInYearJulian(int year) { 
// Modifier le code ci-dessous 
    if (isLeapYearJulian(year)) { 
     return 366; 
    } 
    else { 
     return 365; 
    } 
} 

public static int daysInYearGregorian(int year) { 
// Modifier le code ci-dessous 
    if (isLeapYearGregorian(year)) { 
     return 366; 
    } 
    else { 
     return 365; 
    } 
} 

我想這就是你正試圖在這裏做

0

對於測試博奧利安把它直接在if:這裏

if (boolVariable) { 
    ... 
} 

然而YHE的問題是不同的 - 應該有實際的函數調用所以需要括號和參數:

if (isLeapYearJulian(year)) { 
    ... 
} 
1

的問題是,你沒有調用函數(請參閱https://stackoverflow.com/users/1361491/gjhuizing的答案)。

此外,您可以簡化代碼:

public static boolean isLeapYearJulian(int year) { 
    return (year % 4 == 0); 
} 

public static boolean isLeapYearGregorian(int year) { 
    if (year % 400 == 0) return true; 
    if (year % 100 == 0) return false; 
    return (year % 4 == 0); 
} 

// EXERCICE 2 QUESTION 2        
public static int daysInYearJulian(int year) { 
    return isLeapYearJulian(year) ? 366 : 365; 
} 

public static int daysInYearGregorian(int year) { 
    return isLeapYearGregorian(year) ? 366 : 365; 
} 
0

isLeapYearjulian不是一個變量這裏,
其引用的函數名稱的標識:
更正它:

public static int daysInYearJulian(int year) { 
     if (isLeapYearJulian(year) == true) { 
      return 366; 
     } 
     else { 
      return 365; 
     } 
    } 
0

首先,我想告訴你,你的代碼最底部有一個重音標記(`)。不管怎麼說,我可以看到不對的唯一的事情爲t

// EXERCICE 2 QUESTION 2 
public static int daysInYearJulian(int year) { 
// Modifier le code ci-dessous 
    if (isLeapYearJulian == true) { 
     return 366; 
    } 
    else { 
     return 365; 
    } 
} 

public static int daysInYearGregorian(int year) { 
// Modifier le code ci-dessous 
    if (isLeapYearGregorian == true) { 
     return 366; 
    } 
    else { 
     return 365; 
    } 
}` 

不管怎麼說,我可以看到不對的唯一的一點是你在過去的兩個方法,

public static int daysInYearJulian(int year) { 
// Modifier le code ci-dessous 
    if (isLeapYearJulian == true) { 
    return 366; 
    } 
    else { 
     return 365; 
    } 
} 

public static int daysInYearGregorian(int year) { 
// Modifier le code ci-dessous 
    if (isLeapYearGregorian == true) { 
     return 366; 
    } 
    else { 
     return 365; 
    } 
} 

是他們每個人都在尋找,並試圖檢查名爲'isLeapYearGregorian'和'isLeapYearJulian'的VARIABLES的值。如果你希望你的程序來檢查你的方法返回的值,則必須使用括號像這樣「叫」你的方法:

isLeapYearJulian() 

isLeapYearGregorian() 

如果您修復代碼應工作你的if else語句。

相關問題