| | |
Even / Odd code HELP!!
![]() |
•
•
Join Date: Sep 2004
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2004
Posts: 1
Reputation:
Solved Threads: 0
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: 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.
Java Syntax (Toggle Plain Text)
if ( number%2 == 0/2 );
"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.
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
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
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
![]() |
Similar Threads
- Odd/Even Code (Java)
- Odd And Even (Java)
Other Threads in the Java Forum
- Previous Thread: reading a file into code
- Next Thread: writing to a file
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






