How do i use an array from my main in a different class. I'm making a game where I need to have a single die roll three times. I then take each value and make comparisons like do 2/3 numbers match. if then add to 12 then give the player the pot back. This is the class where i want to use the info from the array. I learnt about arrays and loops last night so i dont really know what im doing
import java.util.Scanner;
public class Game {
private double Bet;
private double Pot;
private double TotalPot;
public void Pot( ){
Pot = 50;
}
public void inputBet( ) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the current month in numerical format: ");
Bet = keyboard.nextDouble();
if (Bet <= Pot) {
System.out.println("Error, Bet out of range");
inputBet();
}
else if (Bet == 0) {
System.out.println("Thank you for playing");
}
}
public void inputEnd( ){
}
public void removeBet( ){
TotalPot = Bet - Pot;
}
public void dieComparison1(){
if ((die[0] == die[1]) || (die[0] == die[2])){
TotalPot = (Bet * 2) + Pot;
}
}
public void print(){
System.out.println(+ TotalPot);}
}
This is my main where the array is created.
public class Assign3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Game smith = new Game();
//smith.Pot();
//smith.inputBet();
int[] die = new int[3];
Die bob = new Die();
int total = 0;
for(int i = 0; i < 3; i++){
die[i] = bob.rollDie();
bob.displayDie(die[i]);
total = total + die[i];
}
bob.displayDie(total);
}
}
In the above code you have posted you in the main
method your have commented out lines where you had created an instance of Game
class:
//Game smith = new Game();
//smith.Pot();
//smith.inputBet();
as you have first created an instance of Game
and stored it in variable smith
and then you called the methods Pot
and inputBet
methods on smith
variable, you could declare another method in the Game
class say rollDie
to contain the code that you have for the rolling of the die and called it as smith.rollDie()
from your main
method. This would eliminate the requirement of having the array die
that you have created in the main
to be accessed from the Game
class.
What I mean to say by the above is:
First your create a variable die
of type int[]
in your Game
class and initialise it to a size of three as:
private int[] die = new int[3];
you crate a method say rollDie()
method containing the methods of rolling the die as :
public void rollDie(){
Die bob = new Die();
int total = 0;
for(int i = 0; i < 3; i++){
die[i] = bob.rollDie();
bob.displayDie(die[i]);
total = total + die[i];
}
bob.displayDie(total);
}
Now in your main
method comment all that is not commented and uncomment all that is commented. Then add the line smith.rollDie()
to roll the die and get the results as :
Game smith = new Game();
smith.Pot();
smith.inputBet();
smith.rollDie();
// Here call the other method that are required to play the game like `dieComparision1()` etc.
/*int[] die = new int[3];
Die bob = new Die();
int total = 0;
for(int i = 0; i < 3; i++){
die[i] = bob.rollDie();
bob.displayDie(die[i]);
total = total + die[i];
}
bob.displayDie(total);*/