• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to return two values back to the main from a for loop?

 
Ashley Kin
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that possible? I'm trying to return int weapon and int cWeapon back to the method, but they go back to the for loop first.
Thanks!

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear to me where you are trying to do this...posting 100 lines of code with vague mention of the problem really isn't terribly helpful.

However, a method can only return one thing. an int, a String, a FuBar....

But think of it this way...If I can only hand you one thing, what's to say that one thing doesn't hold other things? For example, I could hand you an egg, or I could hand you an egg carton (both times, I give you one thing), but the egg carton may contain 12 (or more) eggs.

So you may be able to create an object that contains your weapon and cWeapon and return that.
 
Nomaan Butt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i have understood your problem then,
one way to solve it can be to make weapon and cweapon an array, then when the for loop finishes you can retrieve all the values of these variables.
No need of the strange scenario you mentioned.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nomaan Butt wrote:if i have understood your problem then,
one way to solve it can be to make weapon and cweapon an array, then when the for loop finishes you can retrieve all the values of these variables.
No need of the strange scenario you mentioned.


I do not see other way that the idea above.

Array object[]; this should do the work for you.
 
Winston Gutkowski
Bartender
Pie
Posts: 10273
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Kin wrote:Is that possible? I'm trying to return int weapon and int cWeapon back to the method, but they go back to the for loop first.

Fred's quite right, but it seems to me that you're thinking about this game very procedurally:
  • get the player's weapon of choice (a character)
  • get the computer's choice (another character)
  • compare the two with a big 'if' statement
  • log the result (win/loss)

  • There's no doubt that you'll probably arrive at a solution, and it may well work in Java, but it won't be Object-Oriented (which presumably is why you're learning Java).
    The same solution could be written in C or COBOL or a whole other raft of languages that don't use objects or classes.

    You're getting too bogged down in the "how do I do this?"; what you need to think about more is why - because that is the essence to solving problems, in any language.

    One tip for you: Java has a nice little interface called Comparable which may be very helpful. Have a think about a class called Weapon that implements Comparable (so you can compare one Weapon to another).

    Alternatively, it has another nice little class called an 'enum', which allows you to create named constants, viz:(I deliberately haven't included any code - and you will need some to solve your problem properly).

    Either of these is likely to produce a much more "Java-like" solution than what you're currently trying for; and there are probably other ways too.

    The idea is to make objects do the work; not big reams of procedural code.

    Winston

    [EDIT] Having thought about it, the 'enum' solution is probably better, as your particular game is a highly unusual use of Comparable that violates transitivity. It will work for the purposes of the game, but not otherwise.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20512
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    An Object[] could do the trick but it means you'll need to cast the elements. A specialized object may be better. AWT does this as well, with classes like java.awt.Point, java.awt.Dimension and java.awt.Rectangle. They're not needed for parameters, because the separate parts can be used as well (in fact, many methods that take one of these objects are overloaded to also take the separate parts). But for returning poinrts / sizes / bounds they sure are useful.
     
    fred rosenberger
    lowercase baba
    Bartender
    Pie
    Posts: 12100
    30
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's also hard to say what's going on, since we don't have all the code. Where is 'weapon' declared? I see a String declared in the userWeapon method, but I don't see how that can be in scope in your main method...

    Can you post a simple example that demonstrates your issue - one that will compile?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic