开发者

Leap year program problems- cant seem to get it to run right

I am supposed to Design a program that asks the user to enter a year and then determine if that year is a leap year using for methods, but I can't seem to get it to work. It ran the first time I complied, but it did not work the way 开发者_StackOverflow中文版I wanted it to. Now I made some changes and it's not running at all. What am I doing wrong? I must have four methods.

displayInstructions

isLeap

getYear

displayResults

import java.util.Scanner;
public class LeapYearr
{
    public static void main(String[] args)
    {


        //Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

      int Year;
        displayInstructions();
        Year = getYear();
        boolean leapYear;
        leapYear = isLeap (Year);


    }

    public static void displayInstructions()
    {
         System.out.println("This program allows a users to enter a year then" +
                                    " the program determines weather or not the year entered" +
                                    "is leap Year or is not. ");
    }

    public static int getYear()

    {
    Scanner keyboard = new Scanner(System.in);

       int Year;
       System.out.print("Enter a Year of your choice: ");
        System.out.println("");
        System.out.println("");
      Year = keyboard.nextInt();

          while(Year<1000||Year>9999)
          {

              System.out.println("Invalid entry. Year has to be a 4 Digit Number!!");
                   System.out.println("");
               System.out.print("Enter a Year of your choice: ");
                    System.out.println("");
           Year = keyboard.nextInt;
             }



    return Year;
    }

    public static boolean isLeap (int Year)
    {
        if (Year % 4 != 0)
        {
            return false;
        }
        else if (Year % 400 == 0)
        {
            return true;
        }
        else if (Year % 100 == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public static void displayResults( boolean leapYear, int Year)
    {
        if (isLeapYear(Year))
        {
             System.out.println(Year + "is a leap Year.");
        }
        else
        {
             System.out.println(Year + "is not a leap Year.");
        }
    }
}


Your code will not currently compile.

Fix this in your while loop in getYear():

while(Year<1000||Year>9999)
{
    System.out.println("Invalid entry. Year has to be a 4 Digit Number!!");
    System.out.println("");
    System.out.print("Enter a Year of your choice: ");
    System.out.println("");
    Year = keyboard.nextInt(); // you were missing '()' your parenthesis!
}


The logic of checking leap year is wrong in first place. The 'year' is leap if following is true :

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

I have corrected and cleaned up your code. You can do copy paste to use it :)

import java.util.Scanner;
public class LeapYearr
{
    public static void main(String[] args)
    {
        int Year;
        displayInstructions();
        Year = getYear();
        displayResults(isLeap (Year), Year);


    }

    public static void displayInstructions()
    {
         System.out.println("This program allows a users to enter a year then" +
                                    " the program determines weather or not the year entered" +
                                    "is leap Year or is not. ");
    }

    public static int getYear()

    {
    Scanner keyboard = new Scanner(System.in);

       int Year;
       System.out.print("Enter a Year of your choice: ");
        System.out.println("");
        System.out.println("");
      Year = keyboard.nextInt();

          while(Year<1000||Year>9999)
          {

              System.out.println("Invalid entry. Year has to be a 4 Digit Number!!");
                   System.out.println("");
               System.out.print("Enter a Year of your choice: ");
                    System.out.println("");
           Year = keyboard.nextInt();
             }



    return Year;
    }

    public static boolean isLeap (int Year)
    {
        if(Year%400 ==0 || (Year%100 != 0 && Year%4 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static void displayResults( boolean leapYear, int Year)
    {
        if (leapYear)
        {
             System.out.println(Year + " is a leap Year.");
        }
        else
        {
             System.out.println(Year + " is not a leap Year.");
        }
    }
}


  • you initialize keyboard in main without using it
  • you declare Year (by convention: lowercase!) and leapYear before initialization without necessity (not an error, but bad practice).
  • you don't call displayResults (it is only one result - rename it!)
  • the second call to Year = keyboard.nextInt; is missing the parenthesis.
  • Your indentation is a inconsistent mess. Don`t use more than one empty line.
  • The method to call from displayResult is named isLeap, not isLeapYear. Try to read and understand compiler-error-messages.
  • The method displayResult gets a boolean leapYear passed, but checks itself whether it is a leap year or not, and never uses the parameter. Remove it.
  • And since it then isn't used in main, you should remove it there.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜