• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem about random number  RSS feed

 
Derryl Larson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
does anybody have an idea what the problem about my code is? It's about a method reading an integer which is 0 or 1 (a) and comparing it to a random integerfrom 0 to 36 (result) . If booth integers are either odd or even, boolean won is set true. Else, or if the random integer is 0, won stays false. My problem now is that that it will set the boolean treu as long as the random isn't 0, no matter whether it is odd or even...
Code:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're initializing won to true in line 2, but in your description you are saying "won stays false", which looks like you think it should be false instead of true at the beginning.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also something wrong about the equals zero bit. If you are checking whether you have a positive odd number you can use something like
won = number % 2 == 1;
And you don't need any if-elses. But that will go horribly wrong if you ever pass it a negative argument.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!