Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: GuessingGame problem

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default GuessingGame problem

    Hello, just wondering if anyone can help me with this problem. I had write a program which creates 100 random integers, each in the range 1 to 1000, inclusive. The user guesses a number and is told whether or not it is among the random numbers & this continues until the user guess correctly or enters 0 to signal that he/she gives up. The program had to include a static function called member which returns a boolean value indicating whether or not an integer x occurs in an integer array w where x and w are parameters.

    Here is my code:
    class GuessGame 
    {
    	static boolean member(int x, int[] w)//returns true if x occurs in array w
    	{
    		int i=0;
    		while(i<w.length && i!=x)
    			i++;
    		if(i<w.length) //if found
    			return true;
    		else
    			return false;
    	}
    	public static void main(String[] args) 
    	{
    		int[] nums = new int[1000];
    		for(int i=0; i<nums.length; i++)// fill array with random numbers in the range 1..1000
    		{
    			nums[i] = (int)(1+(Math.random()*1000));
    		}
    		System.out.println("Take a guess..");
    		int guess = Console.readInt();
    		while(guess!=0)
    		{
    			if(member(guess, nums))
    			{
    				System.out.println("This number is among the random numbers.");
    				break;
    			}
    			System.out.println("This number is not among the random numbers, try again.");
    			guess = Console.readInt();
    		}
    	}//end main
    }//end class

    When i run this, it keeps saying the number is among the random numbers, even if it's not. Can anyone help me with this? Thank you


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: GuessingGame problem

    		if(i<w.length) //if found
    			return true;

    Think about that for a minute, what you're essentially saying is...
    If 1(and upwards) is less than 1000, return true.

    How does 1 being less than 1000 equate to x being among w?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: GuessingGame problem

    I also suggest reading - Debugging with System.out.println
    Then you can see exactly what is going on..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: GuessingGame problem

    Sorry yes i understand what you mean. I was looking at another example I had done and thats what was embedded in the if statement. What could i use to determine if x is found? Would it be correct to use: if(w[i]==x) return true; ??

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: GuessingGame problem

    Yeah, that would be what you're after.
    Also consider replacing the while loop with a for loop, it's more convenient than storing a counter variable yourself.
    Of course it will function exactly the same as your while, but it's ultimately down to your preference I suppose.

    Regarding your program specification, int[] nums = new int[1000];
    is a waste of memory, as you only need to store 100 not 1000 Integers.

    (int)(1+(Math.random()*1000));
    Deals with doubles, and you're obviously casting it into an int.

    I'd personally recommend using nextInt(int n) method from the Random class for generating random numbers, but again, its up to you .
    Have a go implementing what you suggested, and come back if you get stuck again.

    Random (Java 2 Platform SE v1.4.2)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. The Following User Says Thank You to newbie For This Useful Post:

    Stockholm Syndrome (March 22nd, 2011)

  7. #6
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: GuessingGame problem

    Quote Originally Posted by newbie View Post
    Yeah, that would be what you're after.
    Also consider replacing the while loop with a for loop, it's more convenient than storing a counter variable yourself.
    Of course it will function exactly the same as your while, but it's ultimately down to your preference I suppose.

    Regarding your program specification, int[] nums = new int[1000];
    is a waste of memory, as you only need to store 100 not 1000 Integers.

    (int)(1+(Math.random()*1000));
    Deals with doubles, and you're obviously casting it into an int.

    I'd personally recommend using nextInt(int n) method from the Random class for generating random numbers, but again, its up to you .
    Have a go implementing what you suggested, and come back if you get stuck again.

    Random (Java 2 Platform SE v1.4.2)
    Thanks very much for the help I appreciate it Regarding the loops, the only reason i use a while loop is because my lecturer showed us that way & said its the simplest way Personally, I don't really see much of a difference in simplicity. The program is now running ok, but if i enter a number that isn't found in the array, it throws this exception:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
    at GuessGame.member(GuessGame.java:10)
    at GuessGame.main(GuessGame.java:26)

    any ideas how to fix this?

  8. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: GuessingGame problem

    I'm going to assume you haven't changed much in you're while loop and say the following:
    An array of size 100, has indices ranging from 0 99 inclusive.

    Because you're incrementing i, (i++;) before the if statement, your first pass is going to check index 1 and your last pass of the loop is going to check 100 instead of 99.
    To resolve this, move the increment to the bottom of the loop.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #8
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: GuessingGame problem

    Sorry I dont fully understand. i++ is the only thing present in the loop body, i'm not sure where you mean to put it
    static boolean member(int x, int[] w)
    	{
    		int i=0;
    		while(i<w.length && w[i]!=x)
    		{
    			i++;
    		}
    		if(w[i]==x)
    			return true;
    		else
    			return false;
    	}

  10. #9
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: GuessingGame problem

    Oh i see.
    Well that loop currently does pretty much nothing.
    You need to place the if and else statements inside the while loop.

    What's giving you the error is, you're incrementing i from 0 to 100 inside the while loop, and after you exit, you try and access index 100, which If you have set your array to size 100, doesnt exist as Array indices start from 0 inclusive.
    Last edited by newbie; March 22nd, 2011 at 07:08 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  11. #10
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: GuessingGame problem

    All was needed was to change while(i<w.length.....) to while(i<w.length-1....) . Works perfectly now. Thanks for your help once again

  12. #11
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: GuessingGame problem

    But it doesn't work perfectly, it will only return true if x is equal to index 99 of W.
    Follow what I wrote and you'll get what you want.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Tags for this Thread