PDA

View Full Version : Calling Methods in Guessing Game;



Genesis2kx
March 25, 2008, 04:01 PM
I have this code to complete but the instructions state the main method should be left as is which is as follows:


import javax.swing.JOptionPane;
public class GuessingGame {

public static void main(String[] args){
int guess; // the user’s guess
int target = getnum(); //a random number selected by the computer

do{
guess = getguess(); //gets a guess from the user
checkguess(guess,target); // compares the guess to the target
}while (guess != target); /* keep doing this as long as the user has not
* guessed the target number
*/
}

I get errors on the int target = getnum(); and guess = getguess(); lines.

GuessingGame.java:12: getnum(int) in GuessingGame cannot be applied to ()
int target = getnum(); //a random number selected by the computer

GuessingGame.java:15: getguess(int) in GuessingGame cannot be applied to ()
guess = getguess(); //gets a guess from the user


I have more code below without error so is it safe to assume the instructions are wrong about not changing the main method? It seems the blank parameters ( ) called by the parameters I wrote is the problem in the main method.

icymint3
March 25, 2008, 06:52 PM
yeah, i think the method was declared to take an integer, but youre not passing one to it when its being called

Genesis2kx
March 25, 2008, 08:09 PM
hmm here is the rest of my code. I know the last part is probably wrong but I can fix that once I get to compile it first since its a compile error and not a run-time error.




public static int getnum(int target){
// returns a random integer from 1-100
target = (int) (Math.random() * 100);
return target;
}
public static int getguess(int guess){
// asks the user for their guess, an integer from 1-100
String guessString = JOptionPane.showInputDialog(
"Enter an Integer between 0 and 100: ");
guess = Integer.parseInt(guessString);
// returns the guess
return guess;
}
public static void checkguess(int g, int t){
/** compares the user’s guess, passed in as parameter g, with
* the random target, passed in as parameter t.
* prints a message stating whether the guess is too high, too low,
* or an exact match (thus ending the game)
*/
String result;
if (g < t)
result = "Your guess is too low. Guess again! ";
else if (g > t)
result = " Your guess is too high. Guess again! ";
else if (g == t)
result = "You have guessed RIGHT! Congratulations!!!";

JOptionPane.showMessageDialog(null, result);

}

jagfun
March 25, 2008, 11:32 PM
check out changes to all 3 methods...btw notice the methods are
only static because you are calling them directly
in the static main method, normally you would create an instance and use that

*************
public static void main(String[] args){
GuessingGame newGame = new GuessingGame();
....................
int target = newGame.getnum();
//a random number selected by the computer
.....................

*************


public static int getnum(){
// returns a random integer from 1-100
return (int) (Math.random() * 100);
}

public static int getguess(){
// asks the user for their guess, an integer from 1-100
String guessString = JOptionPane.showInputDialog(
"Enter an Integer between 0 and 100: ");
return Integer.parseInt(guessString);
}

public static void checkguess(int g, int t){
/** compares the user’s guess, passed in as parameter g, with
* the random target, passed in as parameter t.
* prints a message stating whether the guess is too high, too low,
* or an exact match (thus ending the game)
*/
String result = "";
if (g < t)
result = "Your guess is too low. Guess again! ";
else if (g > t)
result = " Your guess is too high. Guess again! ";
else
result = "You have guessed RIGHT! Congratulations!!!";

JOptionPane.showMessageDialog(null, result);
}

Genesis2kx
March 28, 2008, 08:56 PM
Hmm....is there a solution without changing the Main method...The task was to leave it as is and fill in the other 3 methods.

jagfun
March 29, 2008, 02:17 AM
you don't need to touch the main method just use the other 3 methods as shown...I only edited the main to point out that using static methods was not recommended.

Genesis2kx
March 30, 2008, 01:02 PM
Oh zeen. Thanks yute! I see what the prob was now.