Need some help with this java program

Discussion in 'Mac Programming' started by HowieCarson, Apr 30, 2010.

1. HowieCarson macrumors newbie

Joined:
Apr 30, 2010
#1
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.

50 is too small
75 is too big
62 is too small
69 is too big
65 is too big
63 is too small
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.

50 is too big
25 is too small
37 is too small
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;

guess = keyboard.nextInt();

while (guess != x) {

if (guess > x) {

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

guessCount = guessCount + 1;

guess = keyboard.nextInt();

}

else if (guess < x) {

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

guessCount = guessCount + 1;

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.

Joined:
Mar 23, 2008
#2

Joined:
Apr 19, 2010
#3
4. mrbash macrumors 6502

Joined:
Aug 10, 2008
#4
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(){
}

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") );
}