Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HowieCarson

macrumors newbie
Original poster
Apr 30, 2010
1
0
I've been looking at this thing forever and i can't figure out what is going wrong, i need after it finds your score to ask if you want to play another game and i can't see to figure it out, i know it deals with a do while loop.

This is what i want it to look like...

Do you want to play a game? y

I am thinking of a number between 1 and 100. Try to guess it.

What's your guess? 50
50 is too small
What's your guess? 75
75 is too big
What's your guess? 62
62 is too small
What's your guess? 69
69 is too big
What's your guess? 65
65 is too big
What's your guess? 63
63 is too small
What's your guess? 64
You've got it in 7 guesses. That was ok!

Do you want to play another game? y

I am thinking of a number between 1 and 100. Try to guess it.

What's your guess? 50
50 is too big
What's your guess? 25
25 is too small
What's your guess? 37
37 is too small
What's your guess? 43
You've got it in 4 guesses. That was amazing!

Do you want to play another game? n
Goodbye!

Here is the code i have so far
Code:
import java.util.Scanner;

public class Lab4Part2
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner(System.in);

String doYouWantToPlay;

System.out.print("Do you want to play a game? ");
doYouWantToPlay = keyboard.nextLine();

while (doYouWantToPlay.equals ("y") || doYouWantToPlay.equals ("yes")) {


System.out.println();
System.out.println("I am thinking of a number between 1 and 100. Try to guess it.");
System.out.println();

int x = (int)(100 * Math.random ()) + 1;

int guessCount = 0,guess;


System.out.print("What's your guess? ");
guess = keyboard.nextInt();

while (guess != x) {


if (guess > x) {

System.out.println(guess + " is too big!");

guessCount = guessCount + 1;

System.out.print("What's your guess? ");
guess = keyboard.nextInt();

}

else if (guess < x) {

System.out.println(guess + " is too small!");

guessCount = guessCount + 1;

System.out.print("What's your guess? ");
guess = keyboard.nextInt();

}
}

if (guess == x) {

guessCount = guessCount + 1;

if (guessCount == 1) {

System.out.print("You've got it in " + guessCount + " guesses! That was lucky!");

System.out.println("Do you want to play another game? ");
doYouWantToPlay = keyboard.nextLine();

}

else if (guessCount > 1 && guessCount < 5) {

System.out.print("You've got it in " + guessCount + " guesses! That was amazing!");

System.out.println("Do you want to play another game? ");
doYouWantToPlay = keyboard.nextLine();

}

else if (guessCount > 6 && guessCount < 8) {

System.out.print("You've got it in " + guessCount + " guesses! That was ok!");

System.out.println("Do you want to play another game? ");
doYouWantToPlay = keyboard.nextLine();

}

else if (guessCount > 7 && guessCount < 10) {

System.out.print("You've got it in " + guessCount + " guesses! That was pretty bad!");

System.out.println("Do you want to play another game? ");
doYouWantToPlay = keyboard.nextLine();

}

else if (guessCount > 10) {

System.out.print("You've got it in " + guessCount + " guesses! This is not your game!");

System.out.println("Do you want to play another game? ");
doYouWantToPlay = keyboard.nextLine();

}

}


System.out.println("Goodbye!");
}
}
}

I know this may be simple but I'm very new to this and its becoming frustrating.
 
First, you need to move the "guessing" code out of the "main" method. It should go in its own method.

Then use a do-while loop to execute that method:


private static void guessNumber(){
...your guessing code...
}

public static void main(String [] args){
Scanner scan = new Scanner(System.in);
String token = null;
do{
guessNumber();
System.out.println(" Do you wish to play again Y/N");
token = scan.next();

}while( token.equalsIgnoreCase("y") );
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.