d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Java Problem
Add Reply New Topic New Poll
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Apr 2 2013 11:58am
I'm trying to get my program to determine if a number is even or not. If it is even, I want the counter to be 1 number higher. If it is odd, I want the counter to be that number.

There's an error, but I'm not sure what I did wrong.


Here's the section of code:


if (firstNum % 2 = 0)
counter = firstNum + 1;
else
counter = firstNum;

Here's the error message:

ProgD.java:25: error: unexpected type
if(firstNum % 2 = 0)
^
required: variable
found: value
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 2 2013 12:09pm
Quote (ben_graham7 @ Apr 2 2013 01:58pm)
I'm trying to get my program to determine if a number is even or not. If it is even, I want the counter to be 1 number higher. If it is odd, I want the counter to be that number.

There's an error, but I'm not sure what I did wrong.


Here's the section of code:


if (firstNum % 2 = 0)
  counter = firstNum + 1;
else
  counter = firstNum;

Here's the error message:

ProgD.java:25: error: unexpected type
if(firstNum % 2 = 0)
            ^
  required: variable
  found:    value


you are assigning a value rather than checking for equality.

= is for assignment
== is for checking equality

also im not sure what you mean by counter but if you mean you want to keep track of evens vs odds it is much simpler just to create two variables (int evens, odds; respectively) and just increment as evens++; and odds++; when the conditions are true.

This post was edited by AbDuCt on Apr 2 2013 12:11pm
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Apr 2 2013 12:12pm
Quote (AbDuCt @ Apr 2 2013 02:09pm)
you are assigning a value rather than checking for equality.

= is for assignment
== is for checking equality


Thanks.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll