如何要求多个条件为 return true Java
How to ask multiple conditions to return true Java
我正在编写必须满足先决条件的代码,如果所有条件都满足,那么它将 return 为真。我尝试了多个 "if" 语句,但这似乎不起作用。嵌套的 if 语句似乎不是这里的答案,我认为 "else if" 语句不会起作用。我要问的是,执行此操作的正确方法是什么?我是不是写错了 if 语句?
这是我的代码:
public static boolean isLegitimate(int mon, int day, int year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
// TODO 1: Check if a date is valid.
//checks to see if the months are between 1 and 12
if((mon >= 1) && (mon <= 12)) {
}
//checks to see if the years are greater than 1
if (year > 0){
}
//checks to see if the days are between 1 and 31
if ((day >=0) && (day <=31)){
}
//This checks that if the month is February, is divisible by 4 evenly,
//and is divisible by 100 evenly, then the days can not exceed 29
if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0)){
if (day >29){
return false;
}
}
return true;
}
您可以使用 JodaTime API
public static boolean isLegitmate(int mon, int day, int year){
try {
new DateTime().withMonthOfYear(mon).withYear(year).withDayOfMonth(day);
return true;
} catch (Exception e){
return false;
}
}
试试这个:
public static boolean isLegitimate(int mon, int day, int year){
if( (mon >= 1 && mon <= 12) && (year > 0) && (day >=0 && day <=31)){
if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0))
if (day >29)
return false;
return true;
}
}
在您的代码顶部添加一个布尔变量:
bool legit = true;
在每个if
语句中,如果条件为假,则改变legit的值。如果值为真,请不要更改它。
条件结束时,return变量:
return legit;
如果任何检查不合法,该方法将 return 错误。
编辑:Espen 的解决方案更有效(如果不太准确 - 请参阅评论),但我 OR
排除了双重子句:
if((mon < 1) || (mon>12)) return false;
和
if((day < 1) || (day > 31)) return false;
但是请注意,这仍然可以 return 无效日期作为有效日期,例如:6 月 31 日
当检查失败时只是 return false。
如果其中一个先决条件失败,则无需进一步检查。
public static boolean isLegitimate(int mon, int day, int year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
// TODO 1: Check if a date is valid.
//checks to see if the months are between 1 and 12
if(mon<1) return false;
if(mon>12) return false;
//checks to see if the years are greater than 1
if(year<=0) return false;
//checks to see if the days are between 1 and 31
if(day<=0) return false;
if(day>31) return false;
//This checks that if the month is February, is divisible by 4 evenly,
//and is divisible by 100 evenly, then the days can not exceed 29
if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0)){
if (day >29){
return false;
}
}
return true;
}
这会将您的逻辑分解为多个方法。我稍微修改了闰年逻辑。
public class MultipleConditions {
public static boolean isLegitimate(int mon, int day, int year) {
return validMonth(mon) && validYear(year) && validDay(day) && validFebDay(mon, day, year);
}
private static boolean validFebDay(int mon, int day, int year) {
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
if (mon!=2)
return true; // Not in feb
if (year%4 != 0)
return day <= 28; // Not a leap year
if (year%100 == 0) {
return day <= 29; // Divisible by 4, but not a centurial year
}
// Divisible by 4, centurial year, and divisible by 400
if (year%400==0) {
return day <=29;
}
// Divisible by 4, centurial year not divisible by 400
return day <= 28;
}
private static boolean validDay(int day) {
// checks to see if the days are between 1 and 31
return day >= 0 && day <= 31;
}
private static boolean validYear(int year) {
return year > 0;
}
private static boolean validMonth(int mon) {
// checks to see if the months are between 1 and 12
return (mon >= 1) && (mon <= 12);
}
public static void main(String args []) {
System.out.println(isLegitimate(2, 23, 2017));
System.out.println(isLegitimate(2,29,2017));
System.out.println(isLegitimate(3,31,2017));
}
}
只是另一个建议。
它可能不适合这个特定案例,但可以帮助有类似问题的人 ("ask multiple conditions to return true")。
public static boolean isLegitimate(int mon, int day, int year){
return Stream.of(
checkMonth(mon),
checkYear(year),
checkDay(day),
checkFebruary(mon, day, year))
.allMatch(check -> check);
}
我正在编写必须满足先决条件的代码,如果所有条件都满足,那么它将 return 为真。我尝试了多个 "if" 语句,但这似乎不起作用。嵌套的 if 语句似乎不是这里的答案,我认为 "else if" 语句不会起作用。我要问的是,执行此操作的正确方法是什么?我是不是写错了 if 语句?
这是我的代码:
public static boolean isLegitimate(int mon, int day, int year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
// TODO 1: Check if a date is valid.
//checks to see if the months are between 1 and 12
if((mon >= 1) && (mon <= 12)) {
}
//checks to see if the years are greater than 1
if (year > 0){
}
//checks to see if the days are between 1 and 31
if ((day >=0) && (day <=31)){
}
//This checks that if the month is February, is divisible by 4 evenly,
//and is divisible by 100 evenly, then the days can not exceed 29
if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0)){
if (day >29){
return false;
}
}
return true;
}
您可以使用 JodaTime API
public static boolean isLegitmate(int mon, int day, int year){
try {
new DateTime().withMonthOfYear(mon).withYear(year).withDayOfMonth(day);
return true;
} catch (Exception e){
return false;
}
}
试试这个:
public static boolean isLegitimate(int mon, int day, int year){
if( (mon >= 1 && mon <= 12) && (year > 0) && (day >=0 && day <=31)){
if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0))
if (day >29)
return false;
return true;
}
}
在您的代码顶部添加一个布尔变量:
bool legit = true;
在每个if
语句中,如果条件为假,则改变legit的值。如果值为真,请不要更改它。
条件结束时,return变量:
return legit;
如果任何检查不合法,该方法将 return 错误。
编辑:Espen 的解决方案更有效(如果不太准确 - 请参阅评论),但我 OR
排除了双重子句:
if((mon < 1) || (mon>12)) return false;
和
if((day < 1) || (day > 31)) return false;
但是请注意,这仍然可以 return 无效日期作为有效日期,例如:6 月 31 日
当检查失败时只是 return false。 如果其中一个先决条件失败,则无需进一步检查。
public static boolean isLegitimate(int mon, int day, int year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
// TODO 1: Check if a date is valid.
//checks to see if the months are between 1 and 12
if(mon<1) return false;
if(mon>12) return false;
//checks to see if the years are greater than 1
if(year<=0) return false;
//checks to see if the days are between 1 and 31
if(day<=0) return false;
if(day>31) return false;
//This checks that if the month is February, is divisible by 4 evenly,
//and is divisible by 100 evenly, then the days can not exceed 29
if ((mon == 2) && (year%4==0) && (!(year%100==0)) || (year%400==0)){
if (day >29){
return false;
}
}
return true;
}
这会将您的逻辑分解为多个方法。我稍微修改了闰年逻辑。
public class MultipleConditions {
public static boolean isLegitimate(int mon, int day, int year) {
return validMonth(mon) && validYear(year) && validDay(day) && validFebDay(mon, day, year);
}
private static boolean validFebDay(int mon, int day, int year) {
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
if (mon!=2)
return true; // Not in feb
if (year%4 != 0)
return day <= 28; // Not a leap year
if (year%100 == 0) {
return day <= 29; // Divisible by 4, but not a centurial year
}
// Divisible by 4, centurial year, and divisible by 400
if (year%400==0) {
return day <=29;
}
// Divisible by 4, centurial year not divisible by 400
return day <= 28;
}
private static boolean validDay(int day) {
// checks to see if the days are between 1 and 31
return day >= 0 && day <= 31;
}
private static boolean validYear(int year) {
return year > 0;
}
private static boolean validMonth(int mon) {
// checks to see if the months are between 1 and 12
return (mon >= 1) && (mon <= 12);
}
public static void main(String args []) {
System.out.println(isLegitimate(2, 23, 2017));
System.out.println(isLegitimate(2,29,2017));
System.out.println(isLegitimate(3,31,2017));
}
}
只是另一个建议。
它可能不适合这个特定案例,但可以帮助有类似问题的人 ("ask multiple conditions to return true")。
public static boolean isLegitimate(int mon, int day, int year){
return Stream.of(
checkMonth(mon),
checkYear(year),
checkDay(day),
checkFebruary(mon, day, year))
.allMatch(check -> check);
}