PDA

View Full Version : Java help (random number guessing game)




prostuff1
Apr 28, 2006, 12:26 AM
Thanks to everyone that helped with my last question...now i have another :0

to know what the program needs to do go here (http://www.cse.ohio-state.edu/cse201/labs/lab4/lab4.html).

Here is the code i have so far (and it does basically all that it needs to do, but not all):

import java.util.Scanner;
public class Lab4
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); //User input
int x = (int)(100 * Math.random()) + 1; //Generates random number
int userInt = 0; //user input number
char userAns1_1;
int counter = 0;
boolean hehe = true;

System.out.print("Do you want to play a game: ");

while (hehe == true)
{
String userAns1 = keyboard.nextLine();
userAns1_1 = userAns1.charAt(0);
char userAns2 = 'y'; //Compare to Yes or No answer from user

if (userAns1_1 != userAns2)
{
System.out.println("Goodbye!");
hehe = false;
}
else
{
System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n");

while (x != userInt)
{
System.out.print("What's your guess? ");
userInt = keyboard.nextInt();

if (x < userInt)
{
System.out.println(userInt + " is too big");
}

else if (x > userInt)
{
System.out.println(userInt + " is too small");
}
counter = counter + 1;
}
if (counter == 1)
{
System.out.println("You've got it in " + counter + " guesses. That was lucky!");
}
else if (counter >= 2 && counter <= 4)
{
System.out.println("You've got it in " + counter + " guesses. That was amazing!");
}
else if (counter >= 5 && counter <= 6)
{
System.out.println("You've got it in " + counter + " guesses. That was really good!");
}
else if (counter == 7)
{
System.out.println("You've got it in " + counter + " guesses. That was ok!");
}
else if (counter >= 8 && counter <= 9)
{
System.out.println("You've got it in " + counter + " guesses. That was pretty bad!");
}
else
{
System.out.println("You've got it in " + counter + " guesses. This is not your game!");
}
}
}
}
}

I also get an error at the end of this when i end up getting the correct number.

Here is the error message that i get:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:558)
at Lab4.main(Lab4.java:18)

Thanks for any help you guys/gals can give me.



angelneo
Apr 28, 2006, 01:25 AM
Just a suggestion,

perhaps, you can try changing the way you do your first loop. instead of
while (hehe) {
//blah blah
}

do something like
while ((userAns1=keyboard.nextLine())!=null) {
//blah blah
}

jtalerico
Apr 28, 2006, 12:08 PM
You need to break out of that loop....

Here is working code..

import java.util.Scanner;
public class Lab4
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); //User input
int x = (int)(100 * Math.random()) + 1; //Generates random number
int userInt = 0; //user input number
char userAns1_1;
int counter = 0;
boolean hehe = true;

System.out.print("Do you want to play a game: ");
String userAns1 = keyboard.nextLine();
userAns1_1 = userAns1.charAt(0);

while (hehe == true)
{
char userAns2 = 'y'; //Compare to Yes or No answer from user

if (userAns1_1 != userAns2)
{
System.out.println("Goodbye!");
hehe = false;
}
else
{
System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n");

while (x != userInt)
{
System.out.print("What's your guess? ");
userInt = keyboard.nextInt();

if (x < userInt)
{
System.out.println(userInt + " is too big");
}

else if (x > userInt)
{
System.out.println(userInt + " is too small");
}
counter = counter + 1;
}
if (counter == 1)
{
System.out.println("You've got it in " + counter + " guesses. That was lucky!");
break;
}
else if (counter >= 2 && counter <= 4)
{
System.out.println("You've got it in " + counter + " guesses. That was amazing!");
break;
}
else if (counter >= 5 && counter <= 6)
{
System.out.println("You've got it in " + counter + " guesses. That was really good!");
break;
}
else if (counter == 7)
{
System.out.println("You've got it in " + counter + " guesses. That was ok!");
break;
}
else if (counter >= 8 && counter <= 9)
{
System.out.println("You've got it in " + counter + " guesses. That was pretty bad!");
break;
}
else
{
System.out.println("You've got it in " + counter + " guesses. This is not your game!");
break;
}
}
}
}
}

Doctor Q
Apr 28, 2006, 04:50 PM
jtalerico: If you use the CODE tag instead of the QUOTE tag then the spacing of your code will be preserved when you post it.

I fixed it for you here, but remember that tip for the next time.

ChrisBrightwell
Apr 28, 2006, 05:01 PM
You need to break out of that loop....Beat me to it. :)

savar
Apr 28, 2006, 05:22 PM
That huge if block is a waste too. Instead try something like this:

String comments[] = new String[5];
comments[0] = "You're terrible!";
comments[1] = "You're pretty bad!";
etc.
comments[4] = "You're amazing!";

Then to use it:

System.out.println("It took you " + counter + " tries to get the right answer. " + comments[counter/2]; //intentional int divide

Notice that you should ensure counter < comments * 2.

prostuff1
Apr 28, 2006, 10:31 PM
You need to break out of that loop....

Here is working code..

sniped code...

This works great and thanks for the help but unfortunantly i need that whole process to loop and ask the user if they want to play another game.

So the whole process starts and when the user guesses the correct number it outputs the number of guesses and give the little message. After that print out it should ask the user "Do you want to play another game?" and then if the user enters yes the whole process starts over with a new variable and everything. Until the user enters "no" that process will not stop. I can't seem to figure out how to make it keep looping.

Any help would be appreciated.

That huge if block is a waste too. Instead try something like this:

String comments[] = new String[5];
comments[0] = "You're terrible!";
comments[1] = "You're pretty bad!";
etc.
comments[4] = "You're amazing!";

Then to use it:

System.out.println("It took you " + counter + " tries to get the right answer. " + comments[counter/2]; //intentional int divide

Notice that you should ensure counter < comments * 2.

That is fine advice but we have not learned that kind of stuff in class yet and i think the teacher would wonder about it.

Any help would be greatly appreciated.

Thanks

jtalerico
Apr 29, 2006, 08:59 PM
you can make it a method such as

public void playgame(){
your code
}
Then after the user wins or whatever, just call the method again, this.playgame();

jtalerico
Apr 29, 2006, 09:00 PM
The other comment is using an array, it is not that hard! check the API

prostuff1
Apr 29, 2006, 09:46 PM
thanks for the help. I figured it out but i can tell you that is probably not the prettiest thing ever invented. but it does work and get the job done.

Thansk for the help!!