I'm trying to take the output of another method and use it in another method. I know there are other questions that are similar to mine but the solutions that were in those questions never solved my problem, though they did help a little. Here's where I'm stuck (problem is at rewardBet() method):
class Player {
private ArrayList<Card>hand;
private double cash, bet;
//double cash, bet;
public Player(double theCash)
{
cash = theCash;
hand = new ArrayList<Card>();
bet = 0;
}
public double wagerBet()
{
Scanner in = new Scanner(System.in);
System.out.print("Wager a bet: ");
double bet = in.nextDouble();
cash = cash - bet;
System.out.println("You wagered " + bet + ". " + "Now you have " + cash + " cash left.");
return bet;
}
public void rewardBet()
{
bet = wagerBet(); //this is supposed to be taking whatever the user wagered as a bet in the previous method and
cash = cash + (bet * 2); // apply it to this formula in order to mutate the total cash the player has
System.out.println("You now have " + cash + "cash.");
}
Any suggestions as to how to get this bet variable input to carry over?
EDIT, here's the main method like you guys requested:
class BlackJack {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Deck myDeck = new Deck();
myDeck.shuffle();
Player me = new Player(1000);
Player dealer = new Player(0);
Card c = myDeck.dealCard();
me.wagerBet();
System.out.println("Your first card is " + c);
me.hit(c);
c = myDeck.dealCard();
System.out.println("Your next card is " + c);
me.hit(c);
c = myDeck.dealCard();
System.out.println("Your total hand is currently " + me.totalHand() + ".");
System.out.println("Dealer showing " + c);
dealer.hit(c);
c = myDeck.dealCard();
String answer;
System.out.print("Hit or Stay?");
answer = in.nextLine();
while(answer.equals("Hit") || answer.equals("hit"))
{
System.out.println("Your next card is " + c);
me.hit(c);
c = myDeck.dealCard();
System.out.println("Your total hand is currently " + me.totalHand() + ".");
if(me.totalHand() == 21)
{
System.out.println("You win");
me.rewardBet();
System.exit(0);
}
else if(me.totalHand() < 21)
{
System.out.print("Hit or Stay?");
answer = in.nextLine();
}
else{
System.out.println("Player bust.");
System.exit(0);
}}
while(dealer.totalHand() < 17)
{
System.out.println("Dealer draws " + c);
dealer.hit(c);
c = myDeck.dealCard();
System.out.println("Dealer's total hand is currently " + dealer.totalHand() + ".");
if(dealer.totalHand() == 21)
{
System.out.println("Dealer wins.");
System.exit(0);
}
else if(dealer.totalHand() > 21)
{
System.out.println("Dealer bust. You win.");
me.rewardBet();
System.exit(0);
}
}
if(me.totalHand() > dealer.totalHand())
System.out.println("You win!");
me.rewardBet();
if(me.totalHand() < dealer.totalHand())
System.out.println("Loooooser");
if(me.totalHand() == dealer.totalHand())
System.out.println("Push. Nobody wins");
}
}
and to clarify my problem, the wagerBet() method asks for a double input from the user in the form of a bet. If the player wins his hand then the rewardBet() method will reward the player, giving him back the amount he bet plus the reward, hence 'bet * 2'. The problem is the rewardBet() method isn't recognizing the 'bet' input at all, I'm trying to figure out how to make it so. So for example I make a bet of 50, so now I have 950 dollars (1000 is default). I win the round so rewardBet() needs to give me 100 dollars. Right now it isn't giving me anything for winning.