Results 1 to 9 of 9
Thread: Method problems
- 07-17-2010, 08:30 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 7
- Rep Power
- 0
Method problems
Hello, I have an assignment to make a game. I'm having problems getting a method to recognize a number from the main method. I'm sure it's a gross oversight on my part, but I was wondering if somebody could give me a little guidance on my code.
This is a standard Rock, Paper, Scissors game that I can get to run in just the main method, but trying to get it to run using separate methods is giving me issues.
I appreciate all the help anyone can give.import java.util.*;
public class RockPaperScissors
{
public static void main(String [] args)
{
String userInput; //Holds the string for the user input
System.out.println ("Please Enter R for Rock, P for Paper, S for Scissors: ");
Scanner keyboard = new Scanner(System.in);
userInput = keyboard.next();
userInput = userInput.toUpperCase(); //helps to ease the process by converting everything to uppercase.
System.out.println ("Computer throws out a " + computerSwitch());
}
public static String computerSwitch()
{
Random randomNum = new Random(); //uses the random class create a random object
int computerNum;
computerNum = randomNum.nextInt(3); //Randomly generates the number for the computer.
String computer;
switch (computerNum) //Assigns the random numbers to an appropriate letter.
{
case 0:
{
computer = "R"; //Rock
break;
}
case 1:
{
computer = "P"; //Paper
break;
}
case 2:
{
computer = "S"; //scissors
break;
}
default:
{
computer = "ERROR!!!"; //default error.
}
}
return computer;
}
public static void computerIf(String userStr, String compStr)
{
userStr = userInput; //I'm getting the "cannot find symbol" message here.
compStr = computerSwitch();
if (userStr.equals(compStr))
{
System.out.println("TIE!!!");
}
else if (userStr.equals("R"))
{
if (compStr.equals("S"))
System.out.println("Rock crushes Scissors. WINNER!!!");
}
else if (compStr.equals("P"))
{
System.out.println ("Paper covers Rock. LOSER!!!");
}
else if (userStr.equals("P"))
{
if (compStr.equals("S"))
System.out.println ("Scissor cuts Paper. LOSER!!!");
}
else if (compStr.equals("R"))
{
System.out.println ("Paper covers Rock. WINNER!!!");
}
else if (userStr.equals("S"))
{
if (compStr.equals("P"))
System.out.println ("Scissor cuts Paper. WINNER!!");
}
else if (compStr.equals("R"))
{
System.out.println ("Rock crushes Scissors. LOSER!!!");
}
else
{
System.out.println ("Invalid user input.");
}
}
}
Thanks.
- 07-17-2010, 09:05 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
You defined a variable 'userInput' in your main( ... ) method; that means the variable is local to that method and it doesn't exist outside of that method. If you want to make the variable 'known' to other methods in your class you either have to pass that value as a parameter to the other method (and, of course, that other method should accept such a parameter) or you define that variable outside of any method, making it an 'instance variable'.
kind regards,
Jos
- 07-18-2010, 02:50 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 7
- Rep Power
- 0
Ok, so on your advice, I made a different method to collect the user input for the game.
The error I get now states that "RockPaperScissors2.java:117: variable result might not have been initialized" It looks to me that in the nested if else statements that I have initialized it.
import java.util.*;
public class RockPaperScissors2
{
public static void main(String [] args)
{
// System.out.print ( computerIf());
}
public static String userInputting()
{
String userInput; //Holds the string for the user input
System.out.println ("Please Enter R for Rock, P for Paper, S for Scissors: ");
Scanner keyboard = new Scanner(System.in);
userInput = keyboard.next();
userInput = userInput.toUpperCase(); //helps to ease the process by converting everything to upperacse.
return userInput;
}
public static String computerSwitch()
{
Random randomNum = new Random(); //uses the random class create a random object
int computerNum;
computerNum = randomNum.nextInt(3); //Randomly generates the number for the computer.
String computer;
switch (computerNum) //Assigns the random numbers to an appropriate letter.
{
case 0:
{
computer = "R"; //Rock
break;
}
case 1:
{
computer = "P"; //Paper
break;
}
case 2:
{
computer = "S"; //scissors
break;
}
default:
{
computer = "ERROR!!!"; //default error.
}
}
return computer;
}
public static String computerIf(String userStr, String compStr)
{
userStr = userInputting();
compStr = computerSwitch();
String result;
if (userStr.equals(compStr))
{
result = ("TIE!!!");
}
else if (userStr.equals("R"))
{
if (compStr.equals("S"))
result = ("Rock crushes Scissors. WINNER!!!");
}
else if (compStr.equals("P"))
{
result = ("Paper covers Rock. LOSER!!!");
}
else if (userStr.equals("P"))
{
if (compStr.equals("S"))
result = ("Scissor cuts Paper. LOSER!!!");
}
else if (compStr.equals("R"))
{
result = ("Paper covers Rock. WINNER!!!");
}
else if (userStr.equals("S"))
{
if (compStr.equals("P"))
result = ("Scissor cuts Paper. WINNER!!");
}
else if (compStr.equals("R"))
{
result = ("Rock crushes Scissors. LOSER!!!");
}
else
{
result = ("Invalid user input.");
}
return result;
}
}
- 07-18-2010, 03:31 AM #4
Are you arguing with the compiler? My bet would be on the compiler.
Why not make it happy by: String result = null;
- 07-18-2010, 04:33 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 7
- Rep Power
- 0
That helped that specific Error, Norm.
As you can tell, I am still stumbling my way through this. ;)
So when I go to make a call on the computerIf method, I receive this error.
Here is my main method to try to tie it all together. Is it a syntax error? Programming makes my brain bleed, I swear.RockPaperScissors2.java:17: computerIf(java.lang.String,java.lang.String) in RockPaperScissors2 cannot be applied to ()
System.out.println (computerIf());
public static void main(String [] args)
{
userInputting();
System.out.println ("Computer throws out a " + computerSwitch());
System.out.println (computerIf());
}
It will ask me for the input, and will tell me what the computer throws, but it won't give me the win/lose/tie message from the computerIf method.
- 07-18-2010, 05:00 AM #6
The computerIf() method accepts two String parameters--you are passing no parameters to it. Try passing the results of userInputting and computerSwitch to it.
- 07-18-2010, 05:10 AM #7
Member
- Join Date
- Jun 2010
- Posts
- 7
- Rep Power
- 0
Such as this?
I am completely confused on how to input to and call methods apparently. My book feels vague and my professor doesn't really go into detail. He just says it's in the book.userStr = userInputting(userInput);
compStr = computerSwitch(computer);
- 07-18-2010, 05:22 AM #8
No--userInputting and computerSwitch are passed no parameters.
Imagine a function:
Such a method requires two String parameters to be passed to it, just as your function does. In order to pass the methods, we simply call the function in this manner:Java Code:public boolean A(String x, String y) { // Do something with x and y. return true; }
Before you read below, I'd suggest you now try it on your own. If you're stumped, continue on.Java Code:String something = "Some string"; String another = "Another string"; boolean result = A(something,another); // This is where something and another are "passed" to A.
In your case, you will have to pass the result of two functions, as seen below:
Hate to kind of do it for you, but unfortunately if you didn't grasp the A(x,y) example there's little more I can do in the manner of teaching without using your example as a direct reference.Java Code:String user = userInputting(); // Notice how this function gets no parameters passed to it. String computer = computerSwitch(); String result = computerIf(user,computer); System.out.println(result);
- 07-18-2010, 06:57 AM #9
Member
- Join Date
- Jun 2010
- Posts
- 7
- Rep Power
- 0
Thank you Zack, you explanation is much more clear than what I had been taught previously.
I worked it out before looking at your example (I'm here to learn first.).
I am, however, getting the program to ask for my input twice before giving me the answer. I'm going to have to go through and see why this is.
Please Enter R for Rock, P for Paper, S for Scissors:
r
Computer throws a S
Please Enter R for Rock, P for Paper, S for Scissors:
r
Rock crushes Scissors. WINNER!!!
Thanks for everyone's help, it has been very eye-opening for me. I need to get some different books to help me get this better, I think.
Similar Threads
-
Problems with Static toString() method
By RBNSN in forum New To JavaReplies: 6Last Post: 07-09-2010, 10:15 AM -
problems with replace method
By cecily in forum New To JavaReplies: 1Last Post: 08-02-2007, 09:11 PM -
BufferedReader: readLine method problems
By bbq in forum Advanced JavaReplies: 2Last Post: 06-30-2007, 02:27 AM -
Problems with method
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 06-28-2007, 06:49 PM -
Problems with Find method in EJB
By Nick15 in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 05-14-2007, 01:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks