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

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
Thanks to everyone that helped with my last question...now i have another :0

to know what the program needs to do go here.

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

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

macrumors 68000
Jun 13, 2004
1,541
0
afk
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

macrumors 6502
Nov 23, 2005
358
0
You need to break out of that loop....

Here is working code..

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

Administrator
Staff member
Sep 19, 2002
40,077
8,336
Los Angeles
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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
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

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
jtalerico said:
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.

savar said:
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

macrumors 6502
Nov 23, 2005
358
0
you can make it a method such as

Code:
public void playgame(){
your code 
}
Then after the user wins or whatever, just call the method again, this.playgame();
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.