简体   繁体   中英

Printing two integers

This could be simple but i just can't get around it at the moment practice question. I'm just trying to print a few integers together so that output would be

2004 is a leap year

2013 is not a leap year

public class Ex1partA {
       public static void main(String[] args) {
       int year  = 2004;
       if(year%400==0){
           System.out.println("2004 is a leap year");
       }else if(year%100==0){
           System.out.println("2004 is not a leap year");
       }else if(year%4==0){
           System.out.println("2004 is a leap year");
       }else{
           System.out.println("2004 is not a leap year");
       }
   }

   {
        int year1  = 2013;
        if(year1%400==0){
        System.out.println("2013 is a leap year");
        }else if(year1%100==0){
        System.out.println(" 2013 is not a leap year");
        }else if(year1%4==0){
        System.out.println("2013 is a leap year");
        }else{
        System.out.println("2013 is not a leap year");

   }
 }
}

You are over thinking it. Just use a simple if-else like this.

int year = 2004;
if (year % 4 == 0) {
    System.out.println("2004 is a leap year");
} else {
    System.out.println("2004 is not a leap year");
}

int year1 = 2013;
if (year1 % 4 == 0) {
    System.out.println("2013 is a leap year");
} else {
    System.out.println("2013 is not a leap year");
}

You could even move this to a method where you can just pass the year and it'd display the result for you. Something like this

public static void main(String[] args) {

    int year = 2004;
    checkLeapYear(year);

    int year1 = 2013;
    checkLeapYear(year1);
}

private static void checkLeapYear(int year) {
    if (year % 4 == 0) {
        System.out.println(year + " is a leap year");
    } else {
        System.out.println(year + " is not a leap year");
    }
}

You should put the non-static block code into main or make it as static-block to print 2013 is not a leap year.

public class Ex1partA {

public static void main(String[] args) {
    int year = 2004;
    if (year % 400 == 0) {
        System.out.println("2004 is a leap year");
    } else if (year % 100 == 0) {
        System.out.println("2004 is not a leap year");
    } else if (year % 4 == 0) {
        System.out.println("2004 is a leap year");
    } else {
        System.out.println("2004 is not a leap year");
    }

    // Method#1

    {
        int year1 = 2013;
        if (year1 % 400 == 0) {
            System.out.println("2013 is a leap year");
        } else if (year1 % 100 == 0) {
            System.out.println(" 2013 is not a leap year");
        } else if (year1 % 4 == 0) {
            System.out.println("2013 is a leap year");
        } else {
            System.out.println("2013 is not a leap year");

        }
    }
}

// Method#2
static {
    int year1 = 2013;
    if (year1 % 400 == 0) {
        System.out.println("2013 is a leap year");
    } else if (year1 % 100 == 0) {
        System.out.println(" 2013 is not a leap year");
    } else if (year1 % 4 == 0) {
        System.out.println("2013 is a leap year");
    } else {
        System.out.println("2013 is not a leap year");

    }
}

 }

Your question is a little vague, but it appears you have two separate blocks. As in your main method is enclosed in a set of braces and then the next "statement" is enclosed in a separate set of braces. Edit the braces so that the main method enclosed both blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM