aspose file tools*
The moose likes Beginning Java and the fly likes While loop that depends on inputs form another class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While loop that depends on inputs form another class?" Watch "While loop that depends on inputs form another class?" New topic
Author

While loop that depends on inputs form another class?

Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
First, apologies for the long code, I understand if you completely skip over this, just looking for some help in understanding how to implement a while loop in a class that depends on inputs from another class.
As it stands now, my compiler gives me the message that rolls and times may not be initialized. I cannot equal them to "" as it is not compatible, and setting the value to 0 resets the previous inputs and cancels the entire classes of play and rollDie.
Basically I want to ask the user how many times he or she would like to play the game before the game starts in class main and take the user input and use it in class play. What am I missing here?

Panagiotis Kalogeropoulos
Rancher

Joined: May 27, 2011
Posts: 99

Lets start with some basic things:

my compiler gives me the message that rolls and times may not be initialized


When you use a variable inside a method, you must initialize it yourself. This is not the same for object instance variables that take a default value when the object is initialized. See here for more information http://www.coderanch.com/t/579112/java/java/Why-compiler-does-not-provides

classes of play and rollDie


Play and rollDie are not classes, but methods. The only class you have in your code is Craps. Check in the JLS for
classes http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html
and methods http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4

For the problem that concerns you, you want to use the number of times the user wants to play in play method. I see you already hold the input in a variable called times, so the only thing you need to do is pass that variable to the play method. As you will see by reading the above link about methods, you can pass "messages" back and forth from one method to another (in our case from the main method to the play method). So you could say for instance



As you can see, we pass "messages" from one method to the other which we can use to perform various operations. In our small example we simply print the numbers but you can expand that same logic to anything you need your code to do. So you simply need to pass to the play method the input that you have from your user and use that input to instantiate the rolls and times variables accordingly.

Hope that all the above helped you a little bit.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: While loop that depends on inputs form another class?