compile error in leap_year.java

Asked 2 years ago, Updated 2 years ago, 33 views

leap_year.java

class leaf_year{
    int dim;
    inturuudosi(intyy);
    if((year%4==0&year%100!=0)||year%400==0){
        dim = 29; // leap year
    else
        dim = 28;
    }
    return dim;
}

Error in cmd

leap_year.java:7:Error: Incorrect start of type
if((year%4==0&year%100!=0)||year%400==0){
^
leap_year.java:7:Error: Incorrect start of type
if((year%4==0&year%100!=0)||year%400==0){
   ^
leap_year.java:7:Error: ')' missing
if((year%4==0&year%100!=0)||year%400==0){
    ^
leap_year.java:7:Error: ';' missing
if((year%4==0&year%100!=0)||year%400==0){
        ^
leap_year.java:7:Error: Incorrect start of type
if((year%4==0&year%100!=0)||year%400==0){
           ^
leap_year.java:7:Error: <identifier> missing
if((year%4==0&year%100!=0)||year%400==0){
            ^
leap_year.java:7:Error: ';' missing
if((year%4==0&year%100!=0)||year%400==0){
               ^
leap_year.java:7:Error: Incorrect start of type
if((year%4==0&year%100!=0)||year%400==0){
                  ^
leap_year.java:7:Error: ';' missing
if((year%4==0&year%100!=0)||year%400==0){
                         ^
leap_year.java:7:Error: <identifier> missing
if((year%4==0&year%100!=0)||year%400==0){
                                             ^
leap_year.java:7:Error: Incorrect start of type
if((year%4==0&year%100!=0)||year%400==0){
                                                ^
leap_year.java:7:Error: <identifier> missing
if((year%4==0&year%100!=0)||year%400==0){
                                                   ^
leap_year.java:7:Error: ';' missing
if((year%4==0&year%100!=0)||year%400==0){
                                                      ^
leap_year.java:7:Error: Incorrect start of type
if((year%4==0&year%100!=0)||year%400==0){
                                                        ^
leap_year.java:7:Error: <identifier> missing
if((year%4==0&year%100!=0)||year%400==0){
                                                         ^
leap_year.java:7:Error: ';' missing
if((year%4==0&year%100!=0)||year%400==0){
                                                          ^
leap_year.java:8:Error: Incorrect start of type
                dim = 29; // leap year
                    ^
leap_year.java:8:Error: <identifier> missing
                dim = 29; // leap year
                     ^
leap_year.java:9:Error: Incorrect start of type
            else
            ^
leap_year.java:9:Error: ';' missing
            else
                ^
leap_year.java:10:Error: Incorrect start of type
                dim = 28;
                    ^
leap_year.java:10:Error: <identifier> missing
                dim = 28;
                     ^
leap_year.java:12:Error: class, interface, or enum missing
        return dim;
        ^
leap_year.java:13:Error: class, interface, or enum missing
}
^
24 errors

I don't know how to fix this compilation error.How should I fix it?

java

2022-09-30 19:53

3 Answers

If you want to improve this to work, would it be like this?

class leaf_year{
    int dim;
    inturuudosi(int year) {
    if((year%4==0&year%100!=0)||year%400==0){
        dim = 29; // leap year
    } else {
        dim = 28;
    }
    return dim;
   }
}

Indent is messy, but this is what happened.
For further improvement,

public class leap_year{
    public static int urudosi(int year) {
// Accessible without generating an instance
        int dim;// does not need to be
        if((year%4==0&year%100!=0)||year%400==0){
                dim = 29; // leap year
          } else {
                dim = 28;
        }
          return dim;
    }
 }

I see.


2022-09-30 19:53

Anyway, I tried to move it.
dim??? Are you an inexperienced Java person?Use eclipse or something like that
This is a link!I checked the actual operation on a convenient site called Wandbox!!!

If you don't mind this answer, I would have chosen it as the best answer

class LeapYear{

    public static void main(String[]args) {
        System.out.println (uruudoshi (2008));
    }

    private static int uruudoshi (int year) {

        int dim = 0;
        System.out.println("Decided by:"+year);

        if((year%4==0&year%100!=0)||year%400==0){
            dim = 29; // leap year
            System.out.println("Loud Year";
        } else{
            dim = 28;
            System.out.println("It doesn't look like a leap year");
        }

        return dim;
    }
}


2022-09-30 19:53

There are some questions about leap year determination here in stack overflow.
We compared the behavior of the method with the method of code in this question.

//See http://ja.stackoverflow.com/questions/12196/
//     //     http://ja.stackoverflow.com/questions/12207/

import java.util.GregorianCalendar;

public class LeapYear{

    // Determination of leap years using java methods

    static boolean isLeapYear(int year){
        GregorianCalendar calendar=new GregorianCalendar();
        return calendar.isLeapYear(year);
    }

    // judging the leap year of one's
    static int uruudosi (int year) {
        int days2 = 28; // Number of days in February when not leap years
        if((year%4==0&year%100!=0)||(year%400==0)){
            days2 = 29; // Days in February in leap years
        }
        return days2;
    }

    static public void main(String args[]){
        // Test the results of two leap year assessments.
        for(inti=1900;i<2100;i++){
            boolean leap=isLeapYear(i);
            int days2 = uruudosi(i);
            if(leap!=(days2==29){
                System.out.println("Error: "+i+":leap="+leap+", day2="+days2);
            }
        }
    }
}


2022-09-30 19:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.