-2

So I've looked for answers to how to pass values from one method to another in a class. The problem is that they all suggest a major re-write and I am new to Java and was advised not to use some of the suggestions. Anyone know a quick way?

Trying to pass the value of bet around so that it can get to potAmount() for the calculation.

Code:

import java.util.Scanner;

public class BlackJack {
Scanner scan = new Scanner (System.in);
int potG = 100;
int potT;
int bet;

public void betAmount() {
    System.out.println("Enter your bet amount:  ");
    bet = scan.nextInt();
    if (bet > potG)
        betAmount();
    if (bet == 0)
        endGame();
}

public void potAmount() {
    potT = potG + bet;
    System.out.println("Your current pot is " + potT);
    betAmount();
}

public void triplePot() {
    System.out.println("You win TRIPLE your bet");
    bet = bet * 3;
    potAmount();
}

public void win() {
    System.out.println("You win double your bet.");
    bet = bet * 2;
    potAmount();
}

public void lose() {
    System.out.println("You lose your bet.");
    bet = bet * -1;
    potAmount();
}

public void endGame() {
    System.out.println("You end the game with a pot of " + potG);
}
}

Thank you for any help or suggestions.

Ryan_B
  • 11
  • 4
  • 2
    The way you have it written is fine – Lawrence Aiello Nov 12 '14 at 16:35
  • reminds me of my 10th standard – Deval Khandelwal Nov 12 '14 at 16:35
  • The value for bet refused to carry over for the calculation though. Not sure what the problem is with my limited knowledge. – Ryan_B Nov 12 '14 at 16:36
  • What specifically are you having issues with? Are you getting errors? Is the output not what you expected? Details... – scrappedcola Nov 12 '14 at 16:37
  • @Ryan_B then it seems more like a logical error than a programmatical one – Lawrence Aiello Nov 12 '14 at 16:37
  • Your use of member variables and recursion together strikes me as a bit odd, and prone to subtle logic errors. Please be more specific about what "the value for the bet refused to carry over for the calculation" means, as it sounds like you may be approaching this question from the [wrong direction](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)! One good technique is to add temporary `System.out.println(...)` statements to print your variable values at various points, so you can trace where things start to go wrong. – Jason C Nov 12 '14 at 16:42
  • Why were you advised not to use the suggestions? Seems like if you have a problem and someone who knows (by your own admittance) more than you offers a suggestion, then not using the suggestion is something like _preserving your problem_. – Edwin Buck Nov 12 '14 at 16:42
  • (Your use of `bet` as a member variable that is updated as bets are made is perfectly fine and a good start, but it's not possible to find where other logic errors may be since you do not show us how you use this class.) – Jason C Nov 12 '14 at 16:49

1 Answers1

0

If you want to pass a variable into a method it is as simple as this:

int numberToPass = 1;

public void potAmount(Int passedInt){
    int bet = passedInt;
    //use bet to calculate
    //bet will start out as 1, since the passedInt was = 1
}

Now whenever you want to get your bet int into potAmount you can use

potAmount(numberToPass);
Galax
  • 367
  • 1
  • 13