Results 1 to 8 of 8
- 04-12-2011, 03:58 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Determing leap year using "if statement"
I am writing a program to determine whether or not a year is a leap year, using if statements. This is what I have. It compiles with no errors, but doesn't print anything after the year has been entered. Any help will be greatly appreciated.
import java.text.*;
import java.util.*;
import java.lang.*;
class Ch5Pr12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year;
System.out.println("Year: ");
year = scanner.nextInt( );
if (year%4 == 0) {
if (year%10 != 0) {
System.out.print(year + " is a leap year.");
} else {
if (year%400 == 0) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
}
}
- 04-12-2011, 04:02 PM #2
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
Maybe you are entering a non-leap year and since there no other condition other than being
then surely it will print nothing if it is non-leap.if (year%4 == 0)
concentrate on your if statements and conditions, if one is not true then what well happen.Click on REP and add to member reputation, if you find their advices/solutions effective.
- 04-12-2011, 04:06 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
But isn't that what the else statement should do?
- 04-12-2011, 04:07 PM #4
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
and a simple advice for another time, when you want to add a code put it in code form for it to be clearer, and one more thing, when you have one same output for two conditions you can use || between them in the same if statement, and this means if condition1 OR condition2 is true.
in this way
Java Code:if (condition1 || condition2){ // what your want it to perform }Click on REP and add to member reputation, if you find their advices/solutions effective.
- 04-12-2011, 04:09 PM #5
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
Yes it should but if a year doesn't satisfy the condition %4 == 0 then what would it do ?
You did use an else but in a wrong place. remember that if a year doesn't satisfy the first condition it just never enters to its body, and if it doesn't find any other solution it will perform nothing; and this is what's happening with your codeClick on REP and add to member reputation, if you find their advices/solutions effective.
- 04-12-2011, 04:14 PM #6
You should use elseif instead of putting else{if, its very inefficient.
- 04-12-2011, 04:39 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 04-12-2011, 06:49 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
An "if" statement inside a "for" loop?
By soccermiles in forum New To JavaReplies: 18Last Post: 04-20-2010, 03:44 AM -
[SOLVED] Why does the compiler return "not a statement" for this method body please?
By trueblue in forum New To JavaReplies: 3Last Post: 05-25-2009, 08:50 PM -
"Cached Item Was Locked" causing Select Statement: Hibernate + EHCache
By cloutierm in forum Advanced JavaReplies: 0Last Post: 03-15-2009, 11:53 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks