Even / Odd code HELP!!

Reply

Join Date: Sep 2004
Posts: 3
Reputation: Babyblues is an unknown quantity at this point 
Solved Threads: 0
Babyblues Babyblues is offline Offline
Newbie Poster

Even / Odd code HELP!!

 
0
  #1
Sep 9th, 2004
Ok so here is what I have and I am stuck. It is not recognizing whether a number is even or odd. So where do I need to put the remainder operator so that it will recognize if a number is even or odd?

import javax.swing.JOptionPane; // program uses JOptionPane
public class EvenOdd {

/** Creates a new instance of EvenOdd */
public EvenOdd() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String Number; // string entered by user
String result; // a string containing the output

int number; // number to be read
int even; // number if even
int odd; // number if odd

// read number from user as a string
Number = JOptionPane.showInputDialog( null, " Enter a number" );

// convert number from type String to type int
number = Integer.parseInt( Number );

// initialize result to empty String
result = "";

if ( number%2 == 0/2 );
result = result + Number + " is EVEN ";

// Display results
JOptionPane.showMessageDialog( null, result, " Results ", JOptionPane.INFORMATION_MESSAGE );

System.exit ( 0 ); // terminate application

if ( number%2 != 0/2 );
result = result + Number + " is ODD ";

// Display results
JOptionPane.showMessageDialog( null, result, " Results ", JOptionPane.INFORMATION_MESSAGE );

System.exit ( 0 ); // terminate application


} // end method main

} // end class EvenOdd
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 1
Reputation: burgie12 is an unknown quantity at this point 
Solved Threads: 0
burgie12 burgie12 is offline Offline
Newbie Poster

Re: Even / Odd code HELP!!

 
0
  #2
Sep 25th, 2004
I think I've got it. However, I'm very new to Java Programming, so it may not be complete, and you may have questions about it. When you wrote:
  1. if ( number%2 == 0/2 );
nothing else that you wrote after that applied to that if statement. By replacing that with:
"if ( number%2 == 0/2){" and ending the curly braces before your next if statement, I think the code will work properly. I briefly tested the code, and for my intents and purposes, it works. However, I'd check it over and look at it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1,749
Reputation: nanosani is an unknown quantity at this point 
Solved Threads: 55
Team Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: Even / Odd code HELP!!

 
0
  #3
Sep 25th, 2004
  1. import javax.swing.JOptionPane; // program uses JOptionPane
  2. public class EvenOdd {
  3.  
  4. /** Creates a new instance of EvenOdd */
  5. public EvenOdd() {
  6. }
  7.  
  8. /**
  9. * @param args the command line arguments
  10. */
  11. public static void main(String[] args) {
  12. String Number; // string entered by user
  13. String result; // a string containing the output
  14.  
  15. int number; // number to be read
  16. int even; // number if even
  17. int odd; // number if odd
  18.  
  19. // read number from user as a string
  20. Number = JOptionPane.showInputDialog( null, " Enter a number" );
  21.  
  22. // convert number from type String to type int
  23. number = Integer.parseInt( Number );
  24.  
  25. // initialize result to empty String
  26. result = "";
  27.  
  28. if ( number%2 == 0/2 );
  29. result = result + Number + " is EVEN ";
  30.  
  31. // Display results
  32. JOptionPane.showMessageDialog( null, result, " Results ", JOptionPane.INFORMATION_MESSAGE );
  33.  
  34. System.exit ( 0 ); // terminate application
  35.  
  36. if ( number%2 != 0/2 ){
  37. result = result + Number + " is ODD ";
  38. }
  39. // Display results
  40. JOptionPane.showMessageDialog( null, result, " Results ", JOptionPane.INFORMATION_MESSAGE );
  41.  
  42. System.exit ( 0 ); // terminate application
  43.  
  44.  
  45. } // end method main
  46.  
  47. } // end class EvenOdd
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: Even / Odd code HELP!!

 
0
  #4
Sep 27th, 2004
You should havesomething like:
if ( number % 2 == 0 ){
// The number is EVEN
} else
// the number is ODD
}
number % 2 - will result in 0 if the number is divisable (has no remainder) by 2

Modulas operations return any remainder of the number (in your example 2).

So if you wanted to know if a number is divisable by 10, you could write
number % 10 and it will return a 0 for numbers like 10, 20, 100, 300, etc
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC