I'm trying to do this, but I keep getting errors such as "illegal start of expression" or ") expected"
This code is supposed to run through a game the number of times that a user inputs.
I have a method for what a user enters as their "weapon" (i.e rock, paper, scissors) and returns the string weapon and another method for what the computer randomly chooses, which returns cWeapon.
Then I have another method to print who won based on these two weapons.
Should I be approaching this a different way other than methods with parameters?
The compiler is confused whether line 9 is a method call or a method signature. If it is a method call, then omit the parameter types.
Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Joined: May 31, 2011
Jesper de Jong wrote:
John Jai wrote:
You don't have to specify the type when you call the method; leave of the "String".
I thought he didn't have the variables weapon and cWeapon declared previously
Joined: Jul 18, 2011
You are creating too many scanner objects by using for loop unnecesarily . all you have to do is declare a field like this
which can be used by all the methods in the class.
Assuming that your class has a "static" whoWon(String userWeapon, String singleMatchWeapon) method, as told by others, you have call this method just like you called userWeapon and singleMatch methods in the loops
Why have you got all those members static? It is a good idea to have the Scanner object static, But having a static games() method looks suspicious to me. Also you would do better to get the details from the Scanner, store them as some sort of variable, and pass that variable to the method. Rather thantry... or something similar.
An alternative approach: Set up a utility class, like this, with a static Scanner field, like what Harsha Smith showed earlier. Rather than the multiplying method I showed, create methods like public static int nextIntFromKeyboard(...); you can see what such a method contains here. Remember that is not a complete method. If you ever use System.in or similar as a parameter to a Scanner or similar, don’t simply close the Scanner. Otherwise you close System.in and can’t reopen it