java-Leap Year的基础编程

问题描述 投票:-3回答:3

因此,对于基本的编程课程,我们必须编写一个程序来告诉您一年是否为a年。我们没有使用扫描仪方法;相反,今年将是一个争论。我们必须使用一个名为isLaeapYear(int year)的布尔值。这是我的代码

public class LeapYear{
  public static void main(String[ ] args){
    int year = readInt();
     boolean isLeapYear(int year) = ((year %4 == 0) && (year % 100 !=0) || (year % 400 == 0));
    if (isLeapYear)
    {
      return true;
    }
    else {
      return false;
    }


  }
}

出于某种原因,它不会编译表示预计在我的布尔值行中的isLeapYear之后。有什么建议么?谢谢!

java
3个回答
3
投票
boolean isLeapYear(int year) = ((year %4 == 0) && (year % 100 !=0) || (year % 400 == 0));

以上行完全没有意义。您希望它成为一种方法吗?

如果是这样的话:

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

您会这样称呼它:

boolean isLeapYear = isLeapYear(year); //note that the fact that both the boolean
                                       //variable and the method name are 
                                       //identical is coincidence; the variable
                                       //can be named whatever you want (as long
                                       //as it makes sense).

或者您可以做:

if(isLeapYear(year)) {
    ...
} else {
    ...
}

或者如果您只想要boolean变量:

boolean isLeapYear = ((year %4 == 0) && (year % 100 !=0) || (year % 400 == 0));

1
投票

这是您的代码应具有的外观(未经测试):

public class LeapYear{
  //main method (always runs when you compile then run)
  public static void main(String[ ] args){
    //int year = readInt();
    int hardCodedYear = 4;
    System.out.prinln(isLeapYear(hardCodedYear));//method call and print results
  }

  //create method 
  public boolean isLeapYear(int year){
    //check if its leap year (not sure if this is correct)
    if (year %4 == 0) && (year % 100 !=0) || (year % 400 ==0){
      return true;
    }
    return false;
  }

}

0
投票

看起来您在这里遇到了基本的语法错误。 isLeapYear要么是一个函数,要么是一个变量,并且您将两者混在一起。您应该有类似>>的内容

boolean isLeapYear = ((year %4 == 0) && (year % 100 !=0) || (year % 400 == 0));
© www.soinside.com 2019 - 2024. All rights reserved.