File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to cumulate multiple results of a method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to cumulate multiple results of a method?" Watch "How to cumulate multiple results of a method?" New topic

How to cumulate multiple results of a method?

Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
I need to figure out a way to print an overall statistic info list at the bottom, such as
Overall results:
wins =
losses =
ties =
Should I assign number values to ties, wins, and losses, and then return a value back to main and have a sum function?
Any help is appreciated, thanks!

John Jai

Joined: May 31, 2011
Posts: 1776
Could you please explain the program bit more. You run the game for the number of times specified by the user. Say if user gives 10, the for loop in the games() method might run 10 times. But you are having a return statement at each of your conditional clauses. So I think how much time of games user inputs, the run is always going to give one win or one lose or one tie.

Since you have all methods as static - you could have the wins, loses, ties as static counters and print the value in the stat() method.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3152

My Suggestion is- Have different methods for each result you want to compute. Assigning multiple responsibilities to is not an ideal approach.

Multiple responsibilities to a method would make your method/code - more rigid and fragile.

Mohamed Sanaulla | My Blog
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
You would do better to create an enum for RPS and also one for win-lose-tie. You can use the == operator on enum members, and you can put enum members after case in a switch block.
The == sign works on enum members exactly the way it doesn’t work on Strings. You seem to know that because you used equals() elsewhere.
Your long if () lines are suspicious; I had to put new lines in them, otherwise they are difficult to see. If you get lines that long you have to be very careful that you have got all the && and || in the right places.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Just a tip, use "==" only for primitives (int,boolean,double,char, and other with small case). For String,Integer and others use equals! ;)

[] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4605

I disagree. "==" is perfect for all value types which guarantee to have exactly one instance per value. These do not only include primitives, but also enums and custom types which are instance controlled. Such code is less verbose than using the equals() method.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

The == works when you have just the same reference. I have seen tons of erros because people try to compare every object with ==.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4605

And they shouldn't. But they can use them for instance controlled types that guarantee to only have one instance per value. In this case, the equals method need not be overridden, and becomes identical to the == operator.

This does not include the String and Integer classes for example. They don't make this guarantee. Enums on the other hand, do.
I agree. Here's the link:
subject: How to cumulate multiple results of a method?
It's not a secret anymore!