试图找到一种更简单的方法来修复我的代码

时间:2014-11-12 16:33:53

标签: java

所以我已经找到了如何在类中将值从一个方法传递到另一个方法的答案。问题是他们都提出了一个重要的重写,我是Java的新手,并被建议不要使用一些建议。有人知道吗?

尝试传递bet值,以便可以通过potAmount()进行计算。

代码:

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);
}
}

感谢您提供任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

如果要将变量传递给方法,就像这样简单:

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
}

现在,只要你想把你的赌注输入potAmount,就可以使用

potAmount(numberToPass);
相关问题