View Single Post
Old 01-16-2007, 07:50 PM   #1 (permalink)
nohitters
Tilted
 
[Java] So here I am having another problem.

Like before, I'm still a beginner, so kinda keep this simple if possible please. This time I am writing a program where the user inputs an integer and the program decides whether it is even or odd. I know the exact algorithms because I wrote this program 2 years ago in Visual Basic. the section where the 'if statements' are located keeps getting an error when i compile the file. It says the variables are integers and need to be booleans, but when I switch them i get the exact opposite reaction. How can i fix this for the desired output?


// Java Programming Assignment 2.3
// Program used to determine if an integer is even or odd

import javax.swing.JOptionPane; // uses JOptionPane

public class EvenOdd {

// main method begins execution of Java application
public static void main( String args[] )
{
String input; // string entered by user
String output; // string containing result
output = "";


int number; // integer entered
int number2;

boolean numResult; // result calculator


// read integer as String
input = JOptionPane.showInputDialog( null, "Enter any integer.", "INPUT", JOptionPane.QUESTION_MESSAGE );


// convert integer from String to int
number = Integer.parseInt( input );
number2 = number % 2;


if ( number2 = 1 )
numResult = false;

if ( number2 = 0 )
numResult = true;


// determine if even or odd
if ( numResult = true )
output = "Odd.";

if ( numResult = false )
output = "Even.";

// display output
JOptionPane.showMessageDialog( null, "The integer you selected is " + output, "RESULT", JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );

} // end method main

} // end class EvenOdd
__________________
For Sale.
nohitters is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34