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

1. Methods help????

Okay I'm learning about methods for my class. I think I have a very good foundation, but I'm still an amateur. I have an assignment due tonight but I'm having trouble with it. Here is my assignment.

Lab 6
A number-guessing game begins with one player saying, “I’m thinking of a number between x and y, where x is the smaller number and y is the larger number. The other player responds with a guess. The first player then replies, “it’s larger” or “it’s smaller” or “you’ve got it!” This process goes on until the second player guesses the number.
Write a program that plays a similar game, with a player versus a computer.
Your program will generate a random number between 1 and 100. The computer will prompt the user to enter a number within the range of 1 through 100. The player will then enter a guess. The computer will responds with “it’s larger” or “it’s smaller” or “you’ve got it!”, depending on which is appropriate. The game continues until the player guesses the number.
When the player correctly guesses the number, the program should also display a count of the number of guesses.
You should use Java’s Math.random method to generate your random number. Math.random() returns a double that is less than 1 and greater than or equal to zero. Thus, to generate a random integer between 1 and 100, you can use the algorithm:
(int)(1 + Math.random() * 100)
Since the player could possibly be entering a number of guesses, use a method called getGuess() to prompt the player for a guess and return that guess to the main method.
Use a method called processGuess() to determine whether the player’s guess is equal to, larger than or smaller than the generated random number, and display the appropriate message.
Permit the player to play this game a number of times, with the option to quit after each game is completed.

Here is my code I written so far.

```/**
*
*
* CSC 225 - Online
* Lab 6
*/

import java.util.*;

public class GuessingGame
{

public static void main (String[] args)
{
double guess;

guess = getGuess();

}

public static double getGuess(double guess)
{
System.out.println( " Lets play a guesing game!" );
Scanner scan = new Scanner(System.in);

System.out.println( " Pick your number user!" );
guess = scan.nextDouble();

return guess;

}

public static void processGuess ( double guess)
{
double num;

num = (int)(1 + Math.random() * 100);

if( guess == num)
{
System.out.println( " You are right. " );
}
else if ( guess < num)
{
System.out.println( " Its larger. ");
}
else if( guess > num)
{
System.out.println( " Its lower. " );
}

}

}```

Okay I'm trying to test my methods to make sure they work but the text book doesn't really help on how to test them. So I tried just invoking them but I receive an error message when trying to invoke the first method. Please look at my attachment.

javacomplier error.png

Here is my question.... Am I doing this right lol I mean I feel like I might be doing something wrong. And if not can anyone lead me to the right direction.

Thanks again!

2. Re: Methods help????

I receive an error message when trying to invoke the first method
Please copy the full text of the error message and paste it here.

3. Re: Methods help????

Method getGuess in class GuessGame cannont be applied to given types;
required double
found: no arguments
reason: actual and formal arguments list differ in length

The operator that your use here cannot be used for the
type of value you are using if for. You are either
using the wrong type here, or the wrong operator.

I used all doubles because I didn't want to cast any variables but this error message came up.

4. Re: Methods help????

Method getGuess in class GuessGame cannont be applied to given types;
required double
found: no arguments
The compiler is saying you need to use a double argument when you call the getGuess() method. It did not find any arguments being used.

Why does the getGuess() method have an argument? It is not used in the method.

5. Re: Methods help????

Well my assignment states that I need to use a method to retrieve a double from a user and then evaluate that method in my processGuess method.

--- Update ---

Well not really a double but a number from a user.

6. Re: Methods help????

Either add a double as argument in the call to the method
or remove the argument from the definition of the method.

7. Re: Methods help????

I kind understand what your saying. But methods are a little confusing to me. So are you saying when I call the method I should make sure that variable is a double.

8. Re: Methods help????

when I call the method I should make sure that variable is a double.
Look at the definition for the getGuess() method. You need to call it with the arguments that it requires.

The question I have is: Why does getGuess() need a double value passed to it as an argument?

See the tutorial: http://docs.oracle.com/javase/tutori...arguments.html

9. Re: Methods help????

Okay give me a min and let me look at this documentation. And I will get back to you.

Thanks!

10. Re: Methods help????

Okay my application is almost done. I just need a little help with some debugging.

```/**
*
* Gregory B Shavers Jr
* CSC 225 - Online
* Lab 6
*/

import java.util.*;

public class GuessingGame
{

public static void main (String[] args)
{
double guess;
guess = 0 ;
double ans;

Scanner scan = new Scanner(System.in);

do
{

getGuess(guess);
processGuess(guess);

do
{
System.out.print( " Play again? 1 for YES:0 for NO: ");
ans = scan.nextDouble();
System.out.println( " " );
}while(ans != 1 && ans != 0);

}while (guess == 1);

System.out.println( " Thanks for playing. : )" );

}

public static double getGuess(double guess)
{
System.out.println( " " );
System.out.print( " Lets play a guesing game!" );
Scanner scan = new Scanner(System.in);

System.out.print( " I'm thinking of a number between 1-100. Pick your number user! " );
guess = scan.nextDouble();

return guess;

}

public static void processGuess ( double guess)
{
Scanner scan = new Scanner(System.in);

double num = 0;
int i = 1;
int count = 0 ;

num = (int)(1 + Math.random() * 100);

do
{

if( guess == num)
{
System.out.println( " You are right. " );
System.out.println( " The number was " + num );
System.out.println( " The number of guesses was " + count );
break;
}

else if ( guess < num)
{
System.out.println( " The number is larger. ");
System.out.println( " " );
System.out.print( " Please guess again. " );
guess = scan.nextDouble();
}
else if( guess > num)
{
System.out.println( " The number is smaller. " );
System.out.println( " " );
System.out.print( " Please guess again. " );
guess = scan.nextDouble();
}

count++;
}while( guess != num);

}

}```

Here is my output when I run my code.

Lets play a guesing game! I'm thinking of a number between 1-100. Pick your number user! 50
The number is larger.

The number is smaller.

The number is smaller.

The number is smaller.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

Play again? 1 for YES:0 for NO: 1

Thanks for playing. : )

As you can see after guessing my correct answer. It does not display my else statement for a correct answer. I really don't know why. Also when I try to play the game again. It immediately ends my method and ends the game. Norm can you give me a any suggestions?

11. Re: Methods help????

Where does the code change the value of the guess variable that controls the while loop? It is set to 0 and never changed.

Read up on how java passes variables to methods. It passes the value of the variable, not a reference that can be used to change the variable. The value of guess can not be changed in a method.

12. Re: Methods help????

Okay I fixed the guess method. But what about my output not showing.

--- Update ---

I want to let the user know he has the right output when he guesses the correct number.

--- Update ---

Here is my output FYI.

Lets play a guesing game! I'm thinking of a number between 1-100. Pick your number user! 75
The number is larger.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is smaller.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

The number is larger.

Play again? 1 for YES: 0 for NO:

13. Re: Methods help????

Okay I fixed the guess method
Post the new code so we can see what the problem is now.

14. Re: Methods help????

```/**
*
* Gregory B Shavers Jr
* CSC 225 - Online
* Lab 6
*/

import java.util.*;

public class GuessingGame
{

public static void main (String[] args)
{
double guess;
guess = 0 ;
double ans;

Scanner scan = new Scanner(System.in);

do
{

getGuess(guess);
processGuess(guess);

do
{
System.out.print( " Play again? 1 for YES: 0 for NO: ");
ans = scan.nextDouble();
System.out.println( " " );
}while(ans != 1 && ans != 0);

}while (ans == 1);

System.out.println( " Thanks for playing. : )" );

}

public static double getGuess(double guess)
{
Scanner scan = new Scanner(System.in);

System.out.println( " " );
System.out.print( " Lets play a guesing game!" );

System.out.print( " I'm thinking of a number between 1-100. Pick your number user! " );
guess = scan.nextDouble();

return guess;

}

public static void processGuess ( double guess)
{
Scanner scan = new Scanner(System.in);

double num =0 ;
int count = 1 ;

num = (int)(1 + Math.random() * 100);

do
{

if( guess < num)
{
System.out.println( " The number is larger. ");
System.out.println( " " );
System.out.print( " Please guess again. " );
guess = scan.nextDouble();
}
else if ( guess > num)
{
System.out.println( " The number is smaller. " );
System.out.println( " " );
System.out.print( " Please guess again. " );
guess = scan.nextDouble();
}
else if( guess == num)
{
System.out.println( " You are right. " );
System.out.println( " The number was " + num );
System.out.println( " The number of guesses was " + count );

}

count++;
}while( guess != num);

}

}```

15. Re: Methods help????

Where does the new code receive the value returned by the getGuess() method? It needs to assign it to a variable. Then what is done with it?

Where is the value of guess tested after it is read inside the loop in the processGuess() method when there is a wrong answer? What if guess equals num, the while loop will exit.

16. Re: Methods help????

Okay norm I resolved the issue....almost

Here what we have

```
/**
*
* Gregory B Shavers Jr
* CSC 225 - Online
* Lab 6
*/

import java.util.*;

public class GuessingGame
{

public static void main (String[] args)
{
double guess;
guess = 0 ;
double ans;
int num = 0;

Scanner scan = new Scanner(System.in);

do
{

num = (int)(1 + Math.random() * 100);

getGuess(guess);
processGuess(guess,num);

do
{
System.out.print( " Play again? 1 for YES: 0 for NO: ");
ans = scan.nextDouble();
System.out.println( " " );
}while(ans != 1 && ans != 0);

}while (ans == 1);

System.out.println( " Thanks for playing. : )" );

}

public static double getGuess(double guess)
{
Scanner scan = new Scanner(System.in);

System.out.println( " " );
System.out.print( " Lets play a guesing game!" );

System.out.print( " I'm thinking of a number between 1-100. Pick your number user! " );
guess = scan.nextDouble();

return guess;

}

public static void processGuess ( double guess, double num)
{
Scanner scan = new Scanner(System.in);

int count = 0 ;

do
{

count++;

if( guess < num)
{
System.out.println( " The number is larger. ");
System.out.println( " " );
break;
}
else if ( guess > num)
{
System.out.println( " The number is smaller. " );
System.out.println( " " );
break;
}
else if( guess == num)
{
System.out.println( " You are right. " );
System.out.println( " The number was " + num );
System.out.println( " The number of guesses was " + count );
break;
}

}while( guess != num);

}

}```

My problem is now my do loop won't continue looping. I want it to evaluate until it meets the condition.

Here is my output.

Lets play a guesing game! I'm thinking of a number between 1-100. Pick your number user! 1
The number is larger.

Play again? 1 for YES: 0 for NO: 1

Lets play a guesing game! I'm thinking of a number between 1-100. Pick your number user! 100
The number is larger.

Play again? 1 for YES: 0 for NO:

17. Re: Methods help????

Stop writing code and work on the logic. Your changes seem random.

The code still does not save what getGuess() returns.
What does getGuess() do that helps the program execute?

To see what the computer sees when it executes the code, add a println() statement that prints out the values of guess and num just before the while() statement.

18. Re: Methods help????

The getGuess method runs a random number and then ask the user random guess.

19. Re: Methods help????

getGuess method runs a random number and then ask the user random guess.
What does "runs a random number" mean?
What does the program do with the user's random guess?
Hint: it completely ignores what the user entered.

20. Re: Methods help????

Can I through a spanner in the works. Why are you using the double data type (23.98 etc...). It would be better to use an integer.

Also what happens if the user enters 101 or 249? You would need to validate the users input.

I agree with Norm you should stop writing the code and work out the logic on paper.

21. Re: Methods help????

Okay I turned in the assignment but I still wanted to do it the right way.

Okay here is what is supposed to happen. The getmethod() is supposed to run a random number. After running a random number its supposed to ask the user for a guess of that number. For there the processguess() method is supposed to evaluate this method and see if the user ran the method correctly.

22. Re: Methods help????

The getmethod() is supposed to run a random number.
What does "run a random number" mean? If the method generates a number, how is the number made available to the rest of the program?

23. The Following User Says Thank You to Norm For This Useful Post:

Rain_Maker (February 27th, 2013)

24. Re: Methods help????

Okay my HW assignment states that I need to create a guessing game. The computer is supposed to choose a number 1-100. And from that selection the player is supposed to guess that number. Is that more clearer Norm.

25. Re: Methods help????

Generate random number between 1 - 100
Loop if guess wrong
Guess number (Validate guess is within range)
Wrong guess again
End Loop
Right guess

As Norm said work out the logic first then code. It saves you a lot of time and frustration when coding.

26. The Following User Says Thank You to StephenCoyle For This Useful Post:

Rain_Maker (February 27th, 2013)

27. Re: Methods help????

```
/**
*
* Gregory B Shavers Jr
* CSC 225 - Online
* Lab 6
*/

import java.util.*;

public class GuessingGame
{

public static void main (String[] args)
{
double guess;
guess = 0 ;
double ans;
int num = 0;

Scanner scan = new Scanner(System.in);

do
{

num = (int)(1+Math.random()*100);

getGuess(guess);//runs getguess method
processGuess(guess, num);//runs process guess method

do
{
System.out.print( " Correct! ");
System.out.println( " You guess correctly guessed " + num);
System.out.print( " Play again? 1 for YES: 0 for NO: "); //ask user if he/she wants to play again
ans = scan.nextDouble();
System.out.println( " " );
}while(ans != 1 && ans != 0);

}while (ans == 1);

System.out.println( " Thanks for playing. : )" ); //tells user thanks for playing

}

public static double getGuess(double guess)
{

Scanner scan = new Scanner(System.in);

System.out.print( " Lets play a guesing game!" );  //tell user we are playing a game

System.out.print( " I'm thinking of a number between 1-100. Pick your number user! " ); //ask user to pick a number between 1-100
guess = scan.nextDouble();

return guess;

}

public static void processGuess ( double guess, double num)
{
Scanner scan = new Scanner(System.in);

int count = 1 ;

do //evaluate num and guess operation
{

if( guess < num)
{
System.out.println( " The number is larger. "  + num);//tells user guess under num
System.out.print( " Please guess again. ");
guess = scan.nextDouble();
}
else if ( guess > num)
{
System.out.println( " The number is smaller. " + num );//tell user guess is over num
System.out.print( " Please guess again. ");
guess = scan.nextDouble();
}
/**  else
{
System.out.println( " You are right. " );
System.out.println( " The number was " + num );//displays correct guess
System.out.println( " The number of guesses was " + count );  //display user guesses
*/
count++;
}while (guess !=num );

}

}```

I know why my code was not working. And liked you guys said it was my logic.(Its hard for me to learn code with out really writing it) When I was creating if else statements I notice my do while loop was in conflict with my if else statements. What I wanted to know was if there was another better why of writing this loop. I use the do while loop cause it seems more logical to me then a while or for statement. Here is my output if you wanted to see what happens. Thanks Again everyone.

Lets play a guesing game! I'm thinking of a number between 1-100. Pick your number user! 60
The number is larger. 43.0
The number is smaller. 43.0
The number is larger. 43.0
Correct! You guess correctly guessed 43
Play again? 1 for YES: 0 for NO: 1

Lets play a guesing game! I'm thinking of a number between 1-100. Pick your number user! 100
The number is larger. 34.0