变量未正确转移到方法

时间:2014-03-17 01:48:53

标签: java

我试图获取另一种方法的输出并在另一种方法中使用它。我知道还有其他类似于我的问题,但这些问题中的解决方案从未解决过我的问题,尽管它们确实有所帮助。我遇到了问题(问题出在rewardBet()方法):

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

关于如何让这个下注变量输入结转的任何建议?

编辑,这是你们要求的主要方法:

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

}

为了澄清我的问题,wagerBet()方法以赌注的形式要求用户提供双重输入。如果玩家赢了他的手,那么rewardBet()方法将奖励玩家,给他回去他下注的金额加上奖励,因此打赌* 2&#39;。问题是rewardBet()方法不能识别“赌注”。输入,我试图弄清楚如何做到这一点。所以例如我下注50,所以现在我有950美元(默认为1000)。我赢了这一轮,所以rewardBet()需要给我100美元。现在它并没有给我任何胜利。

1 个答案:

答案 0 :(得分:1)

嗯,主要方法的最后一行有一个问题:

if(me.totalHand() > dealer.totalHand())
    System.out.println("You win!");
    me.rewardBet();

你需要将这个主体包装在大括号中 - if语句只在​​print语句中起作用。虽然这似乎不能解决你所描述的问题。

也许您应该考虑完全不同的设计,并避免使用这么多重复的代码。

<强>酒杯

public class BlackJack
{
    private Deck deck;
    private Player me;
    private Player dealer;

    public static void main(String[] args)
    {
        BlackJack game = new BlackJack();
        game.run();
    }

    public BlackJack()
    {
        deck = new Deck();
        deck.shuffle();
        me = new Player("Joe", 1000.0);
        dealer = new Player("Dealer", 0);
    }

    public void run()
    {
        double bet = requestBet(me);

        // Deal your first two cards
        dealNextCard(me, "Your first card is ");
        dealNextCard(me, "Your second card is ");
        me.printHandTotal();

        // Deal dealer's first card
        dealNextCard(dealer, "Dealer showing ");

        while(requestHitOrStay())
        {
            dealNextCard(me, "Your next card is ");
            me.printHandTotal();

            if(me.totalHand() == 21)
            {
                System.out.println(me.getName() + " wins!");
                rewardBet(me, bet);
                System.exit(0);
            }
            else if(me.totalHand() > 21)
            {
                System.out.println(me.getName() + " bust!");
                System.exit(0);
            }
        }

        while(dealer.totalHand() < 17)
        {
            dealNextCard(dealer, "Dealer draws ");
            dealer.printHandTotal();

            if(dealer.totalHand() == 21)
            {
                System.out.println(dealer.getName() + " wins!");
                System.exit(0);
            }
            else if(dealer.totalHand() > 21)
            {
                System.out.println(dealer.getName() + " bust. You win!");
                rewardBet(me, bet);
                System.exit(0);     
            }
        }

        if(me.totalHand() > dealer.totalHand())
        {
            System.out.println("You win!");
            rewardBet(me, bet);
        }
        else if(me.totalHand() < dealer.totalHand())
        {
            System.out.println("Loooooser");
        }
        else
        {
            System.out.println("Push. Nobody wins");
        }
    }

    public boolean requestHitOrStay()
    {
        System.out.print("Hit or Stay? ");
        Scanner in = new Scanner(System.in);
        return in.nextLine().toLowerCase().equals("hit");
    }

    public void dealNextCard(Player p, String prefix)
    {
        Card c = deck.dealCard();
        System.out.println(prefix + c);
        p.addCard(c);
    }

    public double requestBet(Player p)
    {
        Scanner in = new Scanner(System.in);
        double bet = Integer.MAX_VALUE;
        while(bet > p.getCash())
        {
            System.out.print("Wager a bet: ");
            bet = in.nextDouble();
        }
        p.setCash(p.getCash() - bet);
        System.out.println(p.getName() + " wagered " + bet + ". " + "Now they have " + p.getCash() + " cash left.");
        return bet;
    }

    public void rewardBet(Player p, double bet)
    {
        p.setCash(p.getCash() + bet * 2);
        System.out.println(p.getName() + " now has " + p.getCash() + " cash.");
    }
}

<强>播放器

public class Player
{
    private ArrayList<Card> hand;
    private double cash;
    private String name;

    public Player(String playerName, double startingCash)
    {
        hand = new ArrayList<Card>();
        cash = startingCash;
        name = playerName;
    }

    public void addCard(Card c)
    {
        hand.add(c);
    }

    public int totalHand()
    {
        int total = 0;
        for(Card c : hand)
        {
            total += c.getValue();
        }
        return total;
    }

    public void printHandTotal()
    {
        System.out.println(name + "'s' total hand is currently " + totalHand() + ".");
    }

    public String getName()
    {
        return name;
    }

    public double getCash()
    {
        return cash;
    }

    public void setCash(double cash)
    {
        this.cash = cash;
    }
}