Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 28, 2006, 12:26 AM   #1
prostuff1
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
Java help (random number guessing game)

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:
Quote:
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.
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote
Old Apr 28, 2006, 01:25 AM   #2
angelneo
macrumors 68000
 
Join Date: Jun 2004
Location: 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
}
__________________
This is good, isn't it?
angelneo is offline   0 Reply With Quote
Old Apr 28, 2006, 12:08 PM   #3
jtalerico
macrumors 6502
 
Join Date: Nov 2005
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;
				}
			}
		}		
	}
}
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011

Last edited by Doctor Q; Apr 28, 2006 at 04:51 PM. Reason: fixed spacing
jtalerico is offline   0 Reply With Quote
Old Apr 28, 2006, 04:50 PM   #4
Doctor Q
Administrator
 
Doctor Q's Avatar
 
Join Date: Sep 2002
Location: 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.
__________________
Oh do pay attention 007. In the wrong hands, this cylindrical 12-core Mac Pro with three 4K displays, FirePro graphics, and Thunderbolt 2 could be very dangerous.
Doctor Q is offline   0 Reply With Quote
Old Apr 28, 2006, 05:01 PM   #5
ChrisBrightwell
macrumors 68020
 
ChrisBrightwell's Avatar
 
Join Date: Apr 2004
Location: Huntsville, AL
Quote:
Originally Posted by jtalerico
You need to break out of that loop....
Beat me to it.
__________________
"Simplicity is the ultimate sophistication."
ChrisBrightwell is offline   0 Reply With Quote
Old Apr 28, 2006, 05:22 PM   #6
savar
macrumors 68000
 
savar's Avatar
 
Join Date: Jun 2003
Location: District of Columbia
Send a message via AIM to savar
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.
__________________
Mehce
savar is offline   0 Reply With Quote
Old Apr 28, 2006, 10:31 PM   #7
prostuff1
Thread Starter
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
Quote:
Originally Posted by jtalerico
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.

Quote:
Originally Posted by savar
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
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote
Old Apr 29, 2006, 08:59 PM   #8
jtalerico
macrumors 6502
 
Join Date: Nov 2005
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();
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Apr 29, 2006, 09:00 PM   #9
jtalerico
macrumors 6502
 
Join Date: Nov 2005
The other comment is using an array, it is not that hard! check the API
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Apr 29, 2006, 09:46 PM   #10
prostuff1
Thread Starter
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
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!!
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
[FREE] Pixtaword: Word Guessing Game for Instagram BombApp iPhone and iPod touch Apps 0 Mar 17, 2014 06:30 AM
[FREE] A guessing game with a clever twist violetarts iPhone and iPod touch Apps 4 Jan 23, 2014 04:02 AM
Guessing macbook model from serial number WildCard^ MacBook 2 Nov 16, 2013 09:39 AM
Good, Portable Random Number Generator for C++? ArtOfWarfare Mac Programming 19 Jan 10, 2013 05:59 PM
Creating a random number within a URL using Applescript moorbull Mac Programming 1 Jul 19, 2012 08:16 AM

Forum Jump

All times are GMT -5. The time now is 10:39 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC