Ganesh Patekar wrote:Welcome to CodeRanch!
This is what happens when you use equality operator == OR equals() method to compare two Strings. Output:
strOne == strTwo: false
strOne.equals(strTwo): true
Always use equals() method of String to compare contents of two Strings. Don't write that much code in main method creates confusion and also hard to find logical errors, so better create a separate method you can name it getRockPaperScissors() or anything you feel more expressive. Worth Reading click on --> Main Is A Pain Don't declare variables as static unless there is really specific reason for it. I don't think need to declare static here below, you can put this code in method.
Print message before taking input on console for user, asking enter your choice etc. here
Campbell Ritchie wrote:Useful advice from GP
Since you are using single words for the entry, I would suggest you use next() rather than nextLine() as the method used on the Scanner.
I would suggest an alternative to your use of Math#random to choose the computer's move. Create a Random object and use its nextInt method and use the returned value to pick choices out of an arrayI would also suggest an alternative to your if statements: use a switch statement.
There is a much more object‑oriented way to write that sort of game, using enumerated types, but that may take you more time to learn than you have at your disposal.
Ganesh Patekar wrote:Welcome to CodeRanch!
This is what happens when you use equality operator == OR equals() method to compare two Strings. Output:
strOne == strTwo: false
strOne.equals(strTwo): true
Always use equals() method of String to compare contents of two Strings. Don't write that much code in main method creates confusion and also hard to find logical errors, so better create a separate method you can name it getRockPaperScissors() or anything you feel more expressive. Worth Reading click on --> Main Is A Pain Don't declare variables as static unless there is really specific reason for it. I don't think need to declare static here below, you can put this code in method.
Print message before taking input on console for user, asking enter your choice etc. here
Yes you are right about non-static variables declared in class can't be accessed in static method But you can declare and initialize these two within the main method.(but create separate method for all that code as already mentioned).stelios papamichael wrote:the reason i declared my vars as static is because if i do not,i cannot use them inside the main method since that's a static one
run method? would be easier to help you, if you post that code.My scanner variables are outside of my run method(new method inside a new class)so that there are no errors.However,whenever i try to put system.out.println(); outside of the run method,it does not work and
Here OP is assigning value returned by Math.random() which returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. OP is also not casting the value to int so I think there is no possible pitfall as you mentioned here, do you still suggest to use Random? If yes, can you please tell why?.Campbell Ritchie wrote:Create a Random object and use its nextInt method
Carey Brown wrote:Could you repost your code so that we can see the changes that you've made?
Ganesh Patekar wrote:
Yes you are right about non-static variables declared in class can't be accessed in static method But you can declare and initialize these two within the main method.(but create separate method for all that code as already mentioned).stelios papamichael wrote:the reason i declared my vars as static is because if i do not,i cannot use them inside the main method since that's a static one
There is another method equalsIgnoreCase(String anotherString), you might understand what It does by it's name.This is how we should name the method so just by reading it we would come to know what it does.
run method? would be easier to help you, if you post that code.My scanner variables are outside of my run method(new method inside a new class)so that there are no errors.However,whenever i try to put system.out.println(); outside of the run method,it does not work and
Here OP is assigning value returned by Math.random() which returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. OP is also not casting the value to int so I think there is no possible pitfall as you mentioned here, do you still suggest to use Random? If yes, can you please tell why?.Campbell Ritchie wrote:Create a Random object and use its nextInt method
When your plan will be implemented? because I ran your code and kept waiting something to happen on output screen then realised, it might be yet in your plan hahaha. Just jokes a part.stelios papamichael wrote:As for the message before the input,i was planning on doing so afterwards!
The declaration of a Scanner is a field declaration and (probably) initialisation. The print instruction is a statement and statements are not permitted outside methods or similar. Fields must be outside methods because they belong to the object not to the method. If you try declaring things inside methods, they turn into local variables.stelios papamichael wrote:. . . My scanner variables are outside of my run method(new method inside a new class)so that there are no errors.However,whenever i try to put system.out.println(); outside of the run method,i . . .
Ganesh Patekar wrote:I'm glad, It helped you and you made good changes, well done
Only few things, IMO:
Can you tell why did you create separate class MyGame? If you don't have any code in that class you can happily move run method in MyGame class. No need of Logic class.
When your plan will be implemented? because I ran your code and kept waiting something to happen on output screen then realised, it might be yet in your plan hahaha. Just jokes a part.stelios papamichael wrote:As for the message before the input,i was planning on doing so afterwards!
You can write this code in run method, I think no need to declare them in class.
Do you really need to pass String[] args to run method? I don't think so. Never used it. See in below code in MyGame
You can declare these String variables as private final and initialize in class i.e. the place where you declared Scanner sc and String userChoice. And remember name of final variables are written in capital letters, in your case ROCK, PAPER. Other example having two words like EMP_SALARY etc
Campbell Ritchie wrote:
The declaration of a Scanner is a field declaration and (probably) initialisation. The print instruction is a statement and statements are not permitted outside methods or similar. Fields must be outside methods because they belong to the object not to the method. If you try declaring things inside methods, they turn into local variables.stelios papamichael wrote:. . . My scanner variables are outside of my run method(new method inside a new class)so that there are no errors.However,whenever i try to put system.out.println(); outside of the run method,i . . .
When i move my Scanner and userInput variables inside the run method i get an error telling me that Scanner is leaking and it is never closed for some reason.
Furthermore,if i do not pass String[] args to the run method,i also get an error about it.
Campbell Ritchie wrote:Fields must be outside methods because they belong to the object not to the method. If you try declaring things inside methods, they turn into local variables.
Then, how can i print a message to the console before the userInput variable? (if it is possible)
Ganesh Patekar wrote:Ohh yes later you created Logic class MyGame was already there, yes you can have that two classes, I was thinking the alternative solution of main is a pain.
When i move my Scanner and userInput variables inside the run method i get an error telling me that Scanner is leaking and it is never closed for some reason.
It may be warning not error. Never close a Scanner pointing to System.in, not even if Eclipse or any IDE complains about resource leaks. If you close that it closes input stream permanently so you can't take input from user in this running application. Read discussion about closing scanner here
Furthermore,if i do not pass String[] args to the run method,i also get an error about it.
Because doing this will search for run method without parameter so you also have to remove that String[]arg parameter from run method.
Campbell answered you about print statement why it gives error if you write it outside of method or constructor or block.
Campbell Ritchie wrote:Fields must be outside methods because they belong to the object not to the method. If you try declaring things inside methods, they turn into local variables.
I think we better declare variables as close as possible to where they are used. In this case If the scanner is not used anywhere than run method then I think would be good to declare it in run method and yes that becomes local variable. Please Campbell correct me If I'm wrong, if wrong then why?. About String userChoice, It suppose to be in run method because used in run method only so better declare where it is used. Yes I do agree with String rock, paper and scissors these need to be declared outside of method because they belong to object.
Then, how can i print a message to the console before the userInput variable? (if it is possible)
If you keep userChoice declaration in class i.e. outside of run method then just declare String userChoice; like this in class and take input from user like this userChoice = sc.nextLine(); in run method before Math.random() line. Don't forget to write code to print message before taking input from user in run method.
Carey Brown wrote:Excellent job of working through the issues.
Now that you've got it working I feel it's ok to present another approach.
This makes use of switch()'s ability to switch on Strings.
This handles ties.
And eliminates redundant use of println()'s.
I wasn't speaking specifically about the Scanner field, but about fields in general.Ganesh Patekar wrote:. . . Please Campbell correct me If I'm wrong, if wrong then why? . . .
Look! It's Leonardo da Vinci! And he brought a tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|