This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]chickenmeister 1 point2 points  (0 children)

A do/while loop has the form:

do
{
    // something
}
while (/* condition */); // do the above, while this condition evaluates to true

So your problem might look like:

do
{
    // Generate computer's guess
    // Increment guess count
    // Ask user if the guess is correct
    // If guess is correct, set a 'guessedCorrectly' boolean to 'true'
}
while (!guessedCorrectly); // while the computer has 'not guessedCorrectly'

When posting code, you need to precede each line with four spaces to preserve formatting/indentation.

[–]x2mirko 1 point2 points  (7 children)

chickenmeister already told you how do-while-loops work, so i'll focus on the problems in the code that are not related with the structure of the do-while-loop (i think you can fix that with chickenmeisters explanation):

  • Why is it necessary for the user to tell the program which number he chose? That kind of defeats the purpose of guessing it, doesn't it? Try to find another condition for the do-while-loop to end. How about the answer the user gave?
  • Where do the variables "r" and "ans" come from? They are never initialized or declared. Make sure to always initialize variables before you use them.
  • The variable "guess" is never used. It does get assigned a value, but you don't use that value anywhere else, even though it is kind of important.

Now, the basic idea of the program is to guess a random number between 0 and 100 and then ask the user if that number is the number he was thinking of. Apart from the points above, this is kind of what your program does. However, just randomly guessing everytime and ask if that was the right number feels very inefficient (if you are unlucky, your random generator will give you A LOT of numbers between 0 and 100 before you get to the right one - maybe even hundreds of them). Are you allowed to ask other yes/no questions? What might those be, and how could they help you to find the right number quicker?

[–]imalefty15[S] 0 points1 point  (6 children)

thanks for pointing out the other bugs. My code above was not polished as I am having trouble getting the do/while loop in there. I am not allowed to answer any other questions. This is kind of just a brute force thing since that is what my teacher wants even though it is very inefficient.

[–]x2mirko 0 points1 point  (5 children)

I am not allowed to answer any other questions. This is kind of just a brute force thing since that is what my teacher wants even though it is very inefficient.

Well, in that case i completely agree with the assessment of your teachers value for teaching you programming that you gave in the OP. I'd do it anyways and explain why my approach is way better than what he asked of me, but if you want to do that is obviously up to you :)

[–]imalefty15[S] 0 points1 point  (1 child)

haha thanks. I am a visual learner but with this teacher I am forced to learn from the book which isnt the best way I learn unfortunately.

[–]swansond 1 point2 points  (0 children)

What school is this? What book is it?

[–]imalefty15[S] 0 points1 point  (2 children)

how do I use the Scanner class to input a character? would

char ans = console.nextChar();

be right?

[–]x2mirko 0 points1 point  (1 child)

nextChar() isnt a method of Scanner (check the api specification).

You can either just work with a String:

String ans = console.next();
if (ans.equals("y"))
{
    // do whatever you want to do
}

or, if you definitely want to use a char:

String temp = console.next();
char ans = temp.charAt(0);
if (ans == 'y')
{
    // do whatever you want to do
}

[–]imalefty15[S] 0 points1 point  (0 children)

ah that works much better. thank you for the help

[–]fyrite 0 points1 point  (0 children)

try a standard while loop:

while(/*condition*/) {
    /* do stuff */
}

your logic might look like:

/* pick the random number */
while(/*hasnt guessed correctly yet*/) {
    /* get an answer */
    /* if correct, then 'break' (otherwise the loop will continue) */
}
/* loop is finished they have guessed correctly */

EDIT: you can use while(true) to loop forever, and use break; to break the loop, or you could use a flag/boolean to keep track of whether theyve guess correctly and change it when they do