Results 1 to 4 of 4
- 04-02-2012, 04:54 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Why is my condition statement met? (% operator)
I can not figure this out. The task is simple, is number1 a multiple of number2 - so I use the % to find out if there is a remainder.
but look at this
Java Code:package chapter_ii; import java.util.Scanner; public class Multiples { float number1; float number2; boolean multiple; public static void main (String[] args) { int number1; int number2; Scanner input = new Scanner(System.in); System.out.println("Please input two numbers"); number1 = input.nextInt(); number2 = input.nextInt(); Multiples Multi = new Multiples(number1, number2); Multi.calculate(); } public Multiples(int number1, int number2) { this.number1 = (float)number1; this.number2 = (float)number2; } public void calculate() { if ((number1 % number2) == 0.0) multiple = true; else multiple = false; if (multiple = true) System.out.println(number1 + " is a multiple of " + number2); else System.out.println(number2 + " is not a multiple " + number2); System.out.println(number1 + " % " + number2 + " = " + (number1 % number2) ); System.out.println(multiple); } }
Please input two numbers
5
2
5.0 is a multiple of 2.0
5.0 % 2.0 = 1.0
true
whatever I put in I get "true" for the multiple variable... the test I put at the end proves != 0 so why is the condition
Java Code:if ((number1 % number2) == 0.0)
- 04-02-2012, 05:01 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Why is my condition statement met? (% operator)
Don't compare two boolean values with the = operator (that's the assignment operator); use the == operator instead. Even better don't compare a boolean value against a constant boolean value.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 04-02-2012, 07:58 PM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Why is my condition statement met? (% operator)
thank you, I cant believe I missed that - you can see I knew what the equals operator was, because I used it in the first if statement.
so on my second if statement I wrote if (multiple = true) by mistake
I'm surprised that even compiled, because if statements arent really statements they're conditionals (doesnt have a semi colon like statements do)... does this mean that when using an if statement you can say if(a = 3) {do this} and it will treat it as a statement and assign 3 then do the task? thats odd I would have imagined a compile error.
well thats how I learn. thanks I'll try to get better at seeing stuff like this
- 04-02-2012, 09:02 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Why is my condition statement met? (% operator)
An if statement takes a boolean epression; an assignment is an expression and in your case (you assigned true to a boolean variable) it is a boolean expression. You can't do if(a = 3) because a = 3 isn't a boolean expression. Check Java's syntax in the JLS for all the gory details.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03-26-2012, 05:27 PM -
multiplication using increment operator (with out arthematic operator)
By mallikanala in forum New To JavaReplies: 5Last Post: 01-22-2012, 12:02 AM -
Condition ignored.
By Pojahn_M in forum New To JavaReplies: 8Last Post: 12-09-2011, 03:21 AM -
Tenary condition operator
By mwilliams in forum New To JavaReplies: 7Last Post: 05-05-2011, 12:28 PM -
difficulties with IF statement condition
By maas in forum JavaServer Pages (JSP) and JSTLReplies: 11Last Post: 10-12-2010, 09:38 AM
Bookmarks