Results 1 to 5 of 5
  1. #1
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Help with an If/Else program...

    Hello, im new to java and cant seem to find my mistake in this program . Any help would be appreciated. Thank you.

    What I want the program to do - if the 'int' is an odd number I want the program to print that it is an odd number, if the 'int' is even I want to computer to state that it is an even number.


    public class CheckOddEven {
    public static void main(String[] args) {
    int number= 49; // Here i set the value of number
    System.out.println("The number is " + number);
    if (number%2=0) { // Will return true for all integers that are odd because their remainder would be 1
    System.out.println("ODD NUMBER");
    } else {
    System.out.println("EVEN NUMBER");
    }
    }
    }




    This is the error report -

    Compiling the source code....
    $javac /tmp/13469390249656/CheckOddEven.java 2>&1
    /tmp/13469390249656/CheckOddEven.java:6: unexpected type
    required: variable
    found : value
    if (number%2=0) {
    ^
    1 error
    Last edited by ConnorSG1; 09-06-2012 at 10:44 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Help with an If/Else program...

    cant seem to find my mistake
    Please explain what the program does and what you want the program to do.
    If you get errors, please copy and paste the full text here.

    also please wrap the code in code tags. See: BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Re: Help with an If/Else program...

    Thanks for your help but i found the solution to my problem -

    if (number%2=0)

    This was incorrect, it should have been -

    if (number%2==0)

    although i found the answer i dont understand why i have to use == rather than =
    can anyone explain it to me please?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Help with an If/Else program...

    why i have to use == rather than =
    They are different operators for different purposes:
    = is for assignment
    == is to test equality
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •