0

Why doesn't this code work. I am an amateur, started learning java today.

enter image description here

link to java visualiser: http://www.cs.princeton.edu/~cos126/java_visualize/#

this is the code:

public class ClassNameHere {
public static int max(int[] m) {
if ((m % 400 == 0) && (m % 4 == 0) && (m % 100 != 0)) {
System.out.println(m + "is a leap year");
} else {
System.out.println(m + "is not a leap year");

}
}
}
max(2000);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
ml_enthus123
  • 29
  • 1
  • 3

3 Answers3

1

1st thing i would say is that the way you operate with objects is not making much sense... And all begin here:

public static int max(int[] m) {
if ((m % 400 == 0) && (m % 4 == 0) && (m % 100 != 0)) {

Since m is an array of ints, there is no logical way for the compiler to understand what would be divide an array by 4 or take the modulo of that division.... that segment of code is not even posible to be compiled.

2nd. this logic here is not correct:

if ((m % 400 == 0) && (m % 4 == 0) && (m % 100 != 0)) {

it must be

if ((m % 400 == 0) || (m % 4 == 0) && (m % 100 != 0)) {

Example:

public boolean isLeap(final int year) {
    return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Your logic in line if ((m % 400 == 0) && (m % 4 == 0) && (m % 100 != 0))is incorrect. Here is a correct program to check leap year:-

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

                    //year we want to check
                    int year = 2004;

                    //if year is divisible by 4, it is a leap year

                    if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
                            System.out.println("Year " + year + " is a leap year");
                    else
                            System.out.println("Year " + year + " is not a leap year");
            }
    }
     Hope it will help
Gaurav Mahindra
  • 424
  • 2
  • 6
  • 21
0

max method parameter is an array defined as "int[] m" so max(2000) wouldn't work. Besides that, as stated in other answer, you dont have the main function.

Nooblhu
  • 552
  • 15
  • 33