• Post Reply Bookmark Topic Watch Topic
  • New Topic

while (x <= 5 || y <= 5) not working  RSS feed

 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me again ???!!! I need to find that simple solution to make this thing work when x or y hits a total wins of 5. Thanks.

 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The " and it works " was before doing the scoring code.
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried all the wrong ways to add y into the while statement.
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for example, if x=3 and y=4 you want to repeat the loop, right? Because 3<=5 and 4<=5, yes?

Hopefully that should lead you towards the right boolean expression. The one you chose was wrong because when you said "when x or y hits 5 wins" you were talking about when NOT to repeat the loop.

If you really want to know the logical basis behind your misconception, which I should mention is extremely common among beginning programmers, have a look at this Wikipedia article: De Morgan's laws.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why do you think that (x <= 5 || y <= 5) doesn't work? Work out the logic. My suggestion is to say it in plain English, not like "while x is less than or equal to 5" but rather, say it like "while player one (what?)".

If you're still not clear on what I mean, let's take another example. If I were writing a program that would warn me when my gas was running low, I could write something like this:

That code is not very clear though. For one thing, the variable name 'x' does not tell much. More importantly though, the reader has to translate what the expression (x > 3) means. The equivalent code below reveals the intent more clearly:

An even better way to code that is to push the "formula" into its own method:

So, in your case, what does the expression (x <= 5 || y <= 5) really mean in plain English? Once you work out what you really mean to say, it becomes crystal clear why that expression doesn't work.

BTW, per your comment on your line 31:

it seems you're under the impression that the else part is empty. This is not true. In this case, the else part is actually made up of the if statement on line 33 and everything else that follows it up to line 66. Your formatting style is actually misleading you and if this is what your instructor taught you, it's wrong. It's also poor practice to format your nested if-else-if statements that way, IMO, precisely because it can mislead you the way it has. Depending on your IDE settings, if you let the IDE format this code, it could be formatted in any number of different ways, including the following:

or

These structures are all logically equivalent. This last formatting style shows the chain of if-else-if-else-if-else statements as just a serial nesting of plain old if-else statements reminiscent of those Russian nesting dolls
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x represents player 1 and y represents player 2. I am trying to get the game to end when one of the players has 5 wins.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't gone far enough in expressing it in plain English. What's the condition when Player 1 wins? What's the condition when Player 2 wins?

The while statement is really a way of saying "Keep playing while nobody has 5 wins", isn't it? If you agree, then how do you use a while statement to say "Stop when somebody has 5 wins"?
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everywhere I look the break has to be in the do part. With only one <= it stops like it is supposed to but when I add || y <= 5 it does't stop.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The while statement is really a way of saying "Keep playing while nobody has 5 wins", isn't it? If you agree, then how do you use a while statement to say "Stop when somebody has 5 wins"?

Let me rephrase that since I may have just added to your confusion.

In plain English, what does while (x <= 5 || y <= 5) say? Fill in the blanks: WHILE player one ____ OR player two ___, keep playing.

That's not really what you want, right? Paul already gave you a hint with De Morgan's Laws.

You really want to say something like this: WHILE NOT ... (what?), keep playing.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you still don't get it, let's take a different tack.

You said that it works with only one <=. I assume that you mean when you have: while (x <= 5). I think you've confused yourself a little bit by not counting for yourself how many wins player X actually has. That code will only stop when player X has 6 wins, not 5. Understand why this is so.

So, if having 5 wins means that, in plain English, "the player has won the match", what does (x <= 5) mean in plain English?
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means player 1 has less than or equal to 5 wins.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Olsen wrote:It means player 1 has less than or equal to 5 wins.


Jeremy, part of the problem is that you keep wanting to go down to the formula level. Think high level abstract concept. I already gave you an example: (fuelLevel == 0) translates to isEmpty(fuelLevel)

(x <= 5) translates to WHAT?


 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's getting pretty late (or early, if you use De Morgan's Laws ).

I'll give you my answer: (x <= 5) translates to Nothing Significant with respect to your problem.

(x < 5) translates to "Player X has NOT won the match" because he needs exactly 5 to win the match. That's how you translate to plain English. You kept giving a literal translation instead of a conceptual translation. Figure out what your IDEA is first, then the FORMULA will follow.
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or not. I'm a tree climber in feelsLike( -30)/daily then dealing with everything else.

MODERATOR EDIT: I know it can be frustrating but posting profanity is a great way to get on the wrong side of the law in these parts. Take a deep breath, give it a rest for a few minutes and think of something else. When you clear your head, come back and figure it out. It's not that hard. Good luck.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One last hint before I lay this (and myself) to bed: You really want to say "While neither player has won the match, keep playing", don't you? Now you have the IDEA, work out the FORMULA.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've skimmed this topic. I believe you have:

if x <=5 OR y <=5, keep playing.

So let's say x has won 17 games, and y has won 2. you WANT the game to end, but your logic says they should keep playing. Why? because AT LEAST ONE of your two conditions is still true: y <= 5.

What actually has to be true for the game to end?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You might consider using nested if-statements that don't use both checks at the same time. Saves extra comparisons:

or even nested switch statements


Either of these approaches makes it easier to see whether you have covered all possibilities, and makes it easy to add other choices if they ever come into existence.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:
You might consider using nested if-statements that don't use both checks at the same time. Saves extra comparisons:
...
or even nested switch statements
...
Either of these approaches makes it easier to see whether you have covered all possibilities, and makes it easy to add other choices if they ever come into existence.

I guess that's true if you really HAD to do it that way. They are still coyote ugly solutions, if you ask me.

The cleanest solution, IMO, is to use modulo arithmetic. That can be encapsulated in a method of an enum.

 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That assumes that the OP is far enough along to understand classes and enums. But then again, perhaps this is a good time to learn them.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should learn about classes on your first day. You can learn about enums here.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!