• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to stop boolean value going back to original value in While Loop

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Damien O Sullivan wrote:What should I use so to get the program to repeat the same question again?


The quickest thing to do is right now (although it doesn't do anything to make your code cleaner) is to add something that says:

if (answer is not the right one)
// ???

after the user enters the wrong answer again. The ??? needs to be some statement that can affect whether or not the while-loop will continue to execute. That's really all you're missing right now. Again, it's not pretty but it will work.

Edit: I take that back. I see you got it to work somewhat. So all you want to do now is to make it go back and ask the same question again. Then what's the point of putting logic to make it ask a second time? Do you want to keep asking the same question until they get the correct answer or do you only want to give them two chances to get it right?



I want to keep asking them the same question until they get it right, then when they do, I want to generate a new question for them.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damien O Sullivan wrote:I want to keep asking them the same question until they get it right, then when they do, I want to generate a new question for them.


How do they stop if they get tired of playing?
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need some logic to decide whether or not to generate a new set of numbers or just go with what you already have.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Damien O Sullivan wrote:I want to keep asking them the same question until they get it right, then when they do, I want to generate a new question for them.


How do they stop if they get tired of playing?



I haven't tried to code that part yet, stopping the game wasn't mentioned in the brief but I want to try and get around to that bit eventually. Now I'm just having trouble with getting the question to repeat until its answered correctly.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damien O Sullivan wrote:I haven't tried to code that part yet, stopping the game wasn't mentioned in the brief but I want to try and get around to that bit eventually. Now I'm just having trouble with getting the question to repeat until its answered correctly.


I would have started with the ending the game logic first but that's just me.

Anyway, you have to do a bit of a mind shift. Instead of trying to figure out a way to force execution back to where you want it to go, go with the natural flow of the program and tweak something else. Like I said, you need some logic that controls whether or not you're going to generate a new set of numbers. So when you get to the part where you're asking the question, either you're asking a new question or you're asking the same question as the last time you executed the loop.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Damien O Sullivan wrote:I haven't tried to code that part yet, stopping the game wasn't mentioned in the brief but I want to try and get around to that bit eventually. Now I'm just having trouble with getting the question to repeat until its answered correctly.


I would have started with the ending the game logic first but that's just me.

Anyway, you have to do a bit of a mind shift. Instead of trying to figure out a way to force execution back to where you want it to go, go with the natural flow of the program and tweak something else. Like I said, you need some logic that controls whether or not you're going to generate a new set of numbers. So when you get to the part where you're asking the question, either you're asking a new question or you're asking the same question as the last time you executed the loop.



The frustrating part is that when I answer incorrectly once and then answer correctly the second time, the code runs fine. The problem is when it I get the same question wrong more than once.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to clear your mind of whatever solution you have now in there and rethink what you're telling the computer to do.

1. What is the point of asking the same question twice inside the loop? A loop is used to repeat an instruction ("Ask the user the question"). Why would you explicitly repeat that instruction inside a loop? This is your main problem and it's what's throwing you off. DON'T DO IT. You're just making it hard for yourself. Use the loop that you already have to repeat execution!

2. You need some logic inside the loop to determine if you're going to generate a new set of numbers (thus resulting in a new question) or not (results in asking the same question as last time through the loop).

If you can do these two things, you will have solved your problem. Well, apart from not having a way to stop that is.

Example dialog:

What is 1 + 6?
7
Correct!

What is 2 + 4?
3
Incorrect!

What is 2 + 4?
2
Incorrect!

What is 2 + 4?
5
Incorrect!

What is 2 + 4?
6
Correct!

What is 3 + 5?
8
Correct!


Look at the dialog you want to have with the computer and ask:

Where are the boundaries of the loop? Mark at which points the loop starts and at which point the loop ends/repeats for another pass.
Where and when would you check if you need to generate new numbers so the question is different?
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damien, you'll solve this if you follow the advice in my last reply. So before that happens, I'd like to propose something that I think will help you move on with something more than just a solution.

You know how the good athletes study themselves after a game? They go back and watch tape and look for things they could do better. In other words, they do a retrospective. A lot of professional development teams do that these days with regard to their development process. I think individual developers can benefit from retrospecting about the process they went through in coming up with a solution. After learning new things like how things like while-loops and if-then-else statements work, I think novice programmers would benefit from a "do over" and run through of the process of finding the solution. Kind of like how athletes get better by doing more "reps", only in our case as programmers, we're trying to give our minds another "rep" at the problem.

Does that make sense?

I know that this forum is not the ideal way to do something like this because of the inherent delays in the dialog (it would probably take less than 30 minutes if we were sitting together at a computer) but I'd be willing to walk you through if you're up for it.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damien O Sullivan wrote:The frustrating part is that when I answer incorrectly once and then answer correctly the second time, the code runs fine. The problem is when it I get the same question wrong more than once.


And the reason is that you're trying to CODE the solution, rather than listening to the good advice you've been given (especially Junilu's).

My advice in situations like this (and it's ALWAYS the same): StopCoding (←click).

Right now, you're hung up about HOW you're going to do this - and, even more specifically, how you're gong to do this with a while loop - when what you need to do is understand WHAT you have to do.

We're on to a second page of posts now, so we understand what you want to do, but you're still frustrated because you're simply bashing out different permutations because the last one didn't work.

STOP.

I'd also suggest that you turn your computer OFF. Get out a piece of paper and imagine that you have to describe this process to a 10-year old - IN ENGLISH (or your native language). What would your instructions be to him/her?

Programming is NOT about coding. It's about understanding the problem.

HIH

Winston
 
Saloon Keeper
Posts: 5603
214
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whilst I agree with Winston, one of the problems why this topic is halfway page 2
is that there have been quite some repliers, each stumbling over their own feet
in the hurry to supply yet another reply, with all the good intententions of the world.

My suggestion would be, and actually I found this in the 'answering guide' at this site:
let the one who responds first take full responsibility. Only add to a reply if you think
that you have really something of importance to add. That prevents an Op from
getting flooded with questions and advices from a lot of sources.

Greetz,
Piet
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Whilst I agree with Winston, one of the problems why this topic is halfway page 2
is that there have been quite some repliers, each stumbling over their own feet
in the hurry to supply yet another reply, with all the good intententions of the world...


And, whilst I agree with you, it's plain from the replies that Damien is (or appears to be) working "existentially" - ie, there is no plan to what he is doing, and he is not reading (or taking in) the advice that has been given.

@Damien: Programming is an iterative process, but the thought process shouldn't be. You should KNOW what you're going to do before you write your first line of Java code - and that involves spending most of your time not coding.

And if you disagree with my analysis, please feel free to say so.

Winston
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just so you all know, I'm after sorting it anyway, threw in a second while loop inside the if statement
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damien O Sullivan wrote:Just so you all know, I'm after sorting it anyway, threw in a second while loop inside the if statement


Fair enough, but it sounds like a kludge to me. Care to show us the code and explain why?

Winston

PS: I notice you've started a new thread on a similar subject, so I suggest you mark this one "solved".
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic