Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem getting Logical operator to work  RSS feed

 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am setting up a roll of six dice and then checking if any points were scored in the random roll. While everything else is working great, I cant get this series of conditional tests to operate properly, no matter what the roll is the if statement gets performed. Not sure what I am doing wrong here.



I appreciate any help in the matter
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be easier to help if we knew what the rules were that you were coding. I assume you are trying to do something like Farkle...where you only score points if you roll 1's or 5's.

have you really tried all the conditions?


will print "Score". with your logic, EVERY DIE has to be a 1 or a 5 for it to be false. I'm guessing you're using a RNG, and simply haven't gotten lucky enough to have all the dice land on one of those two numbers.
 
Manu Ullas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:It would be easier to help if we knew what the rules were that you were coding. I assume you are trying to do something like Farkle...where you only score points if you roll 1's or 5's.

have you really tried all the conditions?


I agree with Fred. The logic isn't clear.

Also did you use new Random().nextInt(6) to initialize your test values?

Does the rule:

"The player scores if NONE of the dice show a ONE or a FIVE"

cover what you're trying to do there?
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Despite the fact that the Java Tutorials calls that operator "logical" on one page and "conditional" on another, it is really called the conditional and operator.
You realise that (a AND b) AND c EQUALS a AND (b AND c) EQUALS a AND b AND c because AND is an associative operator? (And my use of EQUALS there is un-grammatical shorthand for all three are the same).
So, you can delete most of the () without altering the logic at all.
Also, you might be able to change the ANDs to ORs with de Morgan's laws (I think only one of them applies here):

NOT(a AND b) = NOT a OR NOT b
NOT(a OR b) = NOT a AND NOT b

You haven't been taught about arrays have you? That method would be much easier to write if you had your six scores in an array.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks a lot for your help everyone, truthfully I do not know much about arrays, seems like something I need to learn for this game.

I have made changes to the program:



Ok to clear up, the point of the game so far is to roll a 1 or a 5 to score any points. There is more combos than that, but I am starting here because I figure it would be easier to set up the condition for it.

I rearanged the code to get rid of the != since they seem to create a lot of confusion amongst, well, all of us (for different reasons ofcourse).

For the errors I am getting now, I could have sworn that the || does work with numeric data, I guess it must be something else I just dont understand as of yet.




GreedGame.java:166: operator || cannot be applied to int,int
if ((dieOneInt = 1 || dieOneInt = 5) ||
// performs body if no points were scored
^
GreedGame.java:167: operator || cannot be applied to int,int
(dieTwoInt = 1 || dieTwoInt = 5)
||
^
GreedGame.java:166: operator || cannot be applied to int,int
if ((dieOneInt = 1 || dieOneInt = 5) ||
// performs body if no points were scored
^
GreedGame.java:168: operator || cannot be applied to int,int
(dieThreeInt = 1 || dieThreeInt
= 5) ||
^
GreedGame.java:169: operator || cannot be applied to int,int
(dieFourInt = 1 || dieFourInt =
5) ||
^
GreedGame.java:170: operator || cannot be applied to int,int
(dieFiveInt = 1 || dieFiveInt =
5) ||
^
GreedGame.java:171: operator || cannot be applied to int,int
(dieSixInt = 1 || dieSixInt = 5)
) {
^
7 errors




Thanks a lot for all you help
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it's something else. You use the = operator to assign a value to a variable (that's what you did) and the == operator to compare two values (that's what you wanted to do).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!