Results 1 to 12 of 12
Thread: "if Else"
- 10-16-2010, 02:19 PM #1
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
"if Else"
Morning guys, trying to compile and run program that reads input from user(a year), then test that input to see if its a leap year..DONT KNOW IF YOU GUYS KNOW BUT TO BE A LEAP YEAR HAS TO BE "/4", "/400" , BUT NOT DIVISIBLE BY 100.
Java Code:import java.util.Scanner; public class read { public static void main(String[]args) { double j; double k1; double b1; double p2; Scanner s1 = new Scanner(System.in); // reads user input through keyboard System.out.print("Enter Your Year here : "); j =s1.nextDouble(); k1 = j % 4; b1= j % 100; p2 = j % 400; if (k1 == 0) { //System.out.println(k1); if (p2 == 0 ) { // checks if divisible by 400 if (b1 != 0) System.out.println(p2); System.out.println(b1); } System.out.println("Your are a LEAPYEAR"); } else { System.out.println("Your are not divisibly by 4 OR 400"); } } }Last edited by Bgreen7887; 10-16-2010 at 05:56 PM.
-
So what is your specific question? It seems to be missing from your post above. Is your code not compiling? Are you getting any error messages? These details are important if you want help.
You'll also want to add code tags so your code is readable. To see how to do this, read the first link in my signature below.
-
Another hint: you should enclose every block within curly braces. In other words, if your code has an if/else/while block or any loop, the body of the block should be enclosed in curly braces even if it is only one line long. It will save your tail in many situations -- including this one. And you'll want to line up the tops and bottoms of your blocks to make sure they match.
;)
- 10-16-2010, 04:02 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Thanks again and please excuse the ROOKIE mistakes. I am re compiling right and will "repost" with the new code and any mistakes if any. :D
- 10-16-2010, 04:44 PM #5
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Java Code:import java.util.Scanner; public class leapyear { public static void main(String[]args) { double j; double k1; double b1; double p1; Scanner s1 = new Scanner(System.in); // leapyears user input through keyboard System.out.print("Enter Your Year here : "); j =s1.nextDouble(); k1 = ( j % 4 ); // "leapyear" should divide cleanly by 4 b1= (j % 100 ); // should not be divisibly by 100 p1 = (j % 400); //can't be divided by 100, but by 400 if ( k1 == 0 ) { //System.out.println("you are divisible by four"); } else { //System.out.println("TRY AGAIN!!!"); } if ( b1 >= 0) { //System.out.println("Good thing your not divisible bye 100"); } else { //System.out.println("Sorry we can divide evenly by 100 and thats not good"); } if ( p1 ==0) { System.out.println("YOUR A LEAP YEAR"); } else { System.out.println("NOT A LEAP YEAR!!!"); } // Program running and compiling without any errors.It's not always correctly caluclating though.. // A leap year is a year if it is divisible by 4, unless it is also diviisible bye 100 but not 400. // For instance the year 2000 is a leap year because even though its divisible bye 100, its also divisible bye 400. // Likewise 1900 is not a leap year because of the /100 clause. } }Last edited by Bgreen7887; 10-16-2010 at 05:50 PM. Reason: added code tags
-
Again, please edit your post above to add code tags. If you do this, more folks will be willing to read your posted code and help you, so it's a win-win deal. There's nothing to it, simply place the start code tag: [code] above your code block and the end code tag: [/code] below it.
I'm not sure how else I can state this so you'll believe me and comply with this recommendation. If you need more information so that you'll be convinced please let me know.Last edited by Fubarable; 10-16-2010 at 05:43 PM.
- 10-16-2010, 05:57 PM #7
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
tags applied!
- 10-16-2010, 06:07 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 10-16-2010, 06:22 PM #9
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
hi Jos
i believe im correct. i just want to (check) if the remainders "ZERO", or not. If it is i want to proceed with the rest of my (checks) .
- 10-16-2010, 07:03 PM #10
That's not what you want. This statement will ALWAYS be true, as any positive number run through modulus will always be >= 0.Java Code:if ( b1 >= 0) {
Example:
0 % 100 = 0
99 % 100 = 99
100 % 100 = 0
You probably want to check if it's > 0, not >=.
- 10-16-2010, 07:10 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 10-16-2010, 08:40 PM #12
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
how to override "cancel operation" in "progress bar"
By singswt in forum SWT / JFaceReplies: 2Last Post: 10-08-2009, 11:28 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