While loop that depends on inputs form another class?
Joined: Apr 17, 2012
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?
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.