4 Replies - 268 Views - Last Post: 31 October 2017 - 02:49 AM Rate Topic: -----

#1 mcbush1301  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-October 17

I need advice on how to fix this code

Posted 30 October 2017 - 08:46 PM

Write a program that reads an integer value from the user representing a year.  Determine if the year is a leap year and output either:
xxxx is a leap year  OR
xxxx is not a leap year
where xxxx is the year entered.
A year is a leap year if it is divisible by 4 except when it is also divisible by 100 but not divisible by 400.  If it is not divisible by 4 then it is NOT a leap year. 
For example, 2015 is not a leap year but 2016 is.  2000 is a leap year but 1900 was not a leap year (divisible by 100 but not 400).
Produce an error message for any input value less than 1582 (the year the Gregorian calendar was adopted).

Please help me fix what I have so far.

import java.util.Scanner; 
public class LeapYear_MCB 
{ 
public static void main (String [] args) 
{ 
System.out.println ("This program will determine whether a year is a leap year!"); 

Scanner x = new Scanner (System.in); 
System.out.println ("\nPlease enter a year: "); 

int year = x.nextInt (); 

if (year>=1582 && year % 4==0) 
{ 
if (year % 100!=0 && year % 400==0) 
{ 
System.out.println("\nThe year is a leap year."); 
} 
else 
{ 
System.out.println ("\nThe year is not a leap year.");  
} 
} 
else 
{ 
System.out.println ("\nThe year is invalid for the Gregorian calendar was not used yet."); 
} 
} 
} 




It works except it doesn't display the year when telling whether it was a leap year or not.

This post has been edited by ndc85430: 30 October 2017 - 10:06 PM
Reason for edit:: Added code tags. Please do this yourself in future.


Is This A Good Question/Topic? 0
  • +

Replies To: I need advice on how to fix this code

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: I need advice on how to fix this code

Posted 30 October 2017 - 11:47 PM

You would use

System.out.printf()



with the format specifier "%d" for integer and the variable 'year' in the argument list.
Was This Post Helpful? 1
  • +
  • -

#3 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,477
  • Joined: 13-June 14

Re: I need advice on how to fix this code

Posted 31 October 2017 - 01:55 AM

Of course, you could still use println; you just need to include the year in the string you pass.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3443
  • View blog
  • Posts: 15,601
  • Joined: 20-September 08

Re: I need advice on how to fix this code

Posted 31 October 2017 - 02:07 AM

Your code is incorrect. Clue: enter the year 1583

It won't help you (nor will it help people here to help you) that your code is badly indented/formatted. See
http://technojeeves....-code-formatter
Was This Post Helpful? 1
  • +
  • -

#5 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: I need advice on how to fix this code

Posted 31 October 2017 - 02:49 AM

Your code is kinda wrong... you have a leap year when:


year is divisible by 4 but not 100
or
year is divisible by 400

testing could be done like this:

if (((year % 4 == 0) && (year % 100 != 100)) || (year % 400 == 0))
    year is leap
else
    year is not leap



If you want to test if year is less than 1582 just surround the above with a corresponding if

If you want to also show the year, mention it in the printing function like
System.out.println("Year " + year + " is leap");

This post has been edited by ccdan: 31 October 2017 - 02:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1