Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

program to end if the year is less than 1582.

  Asked By: Harley    Date: Nov 12    Category: Java    Views: 612
  

This is one of my first programs in Java. Can anyone help me figure
out my problem here?
I can't get my program to end if the year is less than 1582.


/**
* Test Leap Year.
*
*/
import cs1.*;

public class LeapYear {
/**
* Main method.
* @param args Command-line arguments, unused
*/
public static void main(String [] args) {

System.out.println("Enter a year:");

int year = Keyboard.readInt();




boolean isLeap = (year >= 1582);
boolean isDivisibleBy4 = (year % 4 == 0);
boolean isDivisibleBy100 = (year % 100 == 0);
boolean isDivisibleBy400 = (year % 400 == 0);

if (isLeap == false)
System.out.println ("It is unknown " + year + " is a leap
year.");



if (isDivisibleBy4 == true)
if (isDivisibleBy100 == false)
if (isDivisibleBy400 == true)
System.out.println(year + " is a Leap Year.");


if (isDivisibleBy4 == true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");

}

}

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Mario Ryan     Answered On: Nov 12

I notice that you use lot of "if" statements alone. i.e:

if(something)
(There should be something her before going to the next if, or use { } between
the next statements if they are dependant on the above one )
if(somethinglese)


As I understand the problem, you need to use else { } between all of what
follows:

if (isLeap == false)
System.out.println ("It is unknown " + year  + " is a leap
year.");


else

{

the rest of the program

}


Try it and let me know if that makes or doesn't make sense.

 
Answer #2    Answered By: Garry Sanchez     Answered On: Nov 12

I haven't gone though your actual logic, to solve your problem.

However ...

When you use if statements always contain the data in curly brackets {}.
While you don't need to by the rules of programming, it always bites you
in the bum if you don't.

I know it can be a pain to curly everything when you don't need to right
now, but if you don't it gets so much harder to find your bugs.

Actualy just a look though, it seems that you are using some sort of
package from Uni that simplifies some actions (import csl.*), so I had
to rework some of it,

Another one of your problems might be this

> boolean isLeap = (year >= 1582);

> if (isLeap == false)
> System.out.println ("It is unknown " + year  + " is a leap
> year.");

This is what gives some wierdness with dates before 1582.

Instead of this:

> if (isDivisibleBy4 == true)
> if (isDivisibleBy100 == false)
> if (isDivisibleBy400 == true)
> System.out.println(year + " is a Leap Year.");

try this

if (isDivisibleBy4 && !isDivisibleBy100 && isDivisibleBy400){
System.out.println(year + " is a Leap Year.");
}

I don't normaly give code out to people learning, but you where on the
right track and probably just need to see some examples to allow your
brain to bend around the problem.

I haven't check to see if this works, but I think it does (or at least
it appears to at first glance. Before anybody jumps on my head for not
using StringBuffer ... I know :)

Below works (apparently), give it a go learn from my comments change it
into using the package your course gives you (so you have to use Keyboard).

 
Answer #3    Answered By: Chung Tran     Answered On: Nov 12

I was able to use your example and expand a
little on that to finish the rest of the code.
Using the && and ! made more sense than the if else.

 
Answer #4    Answered By: Salvador Alexander     Answered On: Nov 12

If you mean that you can't figure out how to make the program  exit
without continuing, use System.exit(int status), where status=0 means
normal termination and status=nonzero means abnormal termination.

 
Didn't find what you were looking for? Find more on program to end if the year is less than 1582. Or get search suggestion and latest updates.




Tagged: