Java 变量未正确转移到方法

Java 变量未正确转移到方法,java,Java,我试图获取另一个方法的输出,并在另一个方法中使用它。我知道还有其他一些问题与我的问题相似,但这些问题中的解决方案并没有解决我的问题,尽管它们确实帮了我一点忙。这就是我被卡住的地方(问题在于rewardBet()方法): 职业玩家{ 私人ArrayListhand; 私人双现金、押注; //双倍现金,押注; 公共玩家(双倍现金) { 现金=现金; hand=newarraylist(); bet=0; } 公共双赌注 { 扫描仪输入=新扫描仪(系统输入); 系统输出打印(“下注:”); 双下注=i

我试图获取另一个方法的输出,并在另一个方法中使用它。我知道还有其他一些问题与我的问题相似,但这些问题中的解决方案并没有解决我的问题,尽管它们确实帮了我一点忙。这就是我被卡住的地方(问题在于rewardBet()方法):

职业玩家{
私人ArrayListhand;
私人双现金、押注;
//双倍现金,押注;
公共玩家(双倍现金)
{
现金=现金;
hand=newarraylist();
bet=0;
}
公共双赌注
{
扫描仪输入=新扫描仪(系统输入);
系统输出打印(“下注:”);
双下注=in.nextDouble();
现金=现金-押注;
System.out.println(“您下注了”+bet+“+”现在您有了“+cash+”cash left.”);
回注;
}
公开无效回扣()
{
bet=wagerBet();//这应该是将用户在上一个方法中下注的任何东西作为赌注,并且
cash=cash+(bet*2);//将其应用于此公式,以改变玩家拥有的现金总量
System.out.println(“您现在有”+现金+“现金”);
}
关于如何将此赌注变量输入结转,有何建议

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

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");
}
class二十一点{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(系统输入);
甲板myDeck=新甲板();
myDeck.shuffle();
玩家me=新玩家(1000);
玩家经销商=新玩家(0);
Card c=myDeck.dealCard();
我。瓦格贝();
System.out.println(“您的第一张卡是”+c);
我打(c);
c=myDeck.dealCard();
System.out.println(“您的下一张卡是”+c);
我打(c);
c=myDeck.dealCard();
System.out.println(“您的总手数当前为“+me.totalHand()+”);
系统输出打印项次(“经销商显示”+c);
经销商。点击(c);
c=myDeck.dealCard();
字符串回答;
系统输出打印(“命中还是停留?”);
answer=in.nextLine();
while(answer.equals(“Hit”)| answer.equals(“Hit”))
{
System.out.println(“您的下一张卡是”+c);
我打(c);
c=myDeck.dealCard();
System.out.println(“您的总手数当前为“+me.totalHand()+”);
if(me.totalHand()==21)
{
System.out.println(“你赢了”);
我。报酬();
系统出口(0);
}
else if(me.totalHand()<21)
{
系统输出打印(“命中还是停留?”);
answer=in.nextLine();
}
否则{
System.out.println(“玩家半身像”);
系统出口(0);
}}
而(dealer.totalHand()<17)
{
系统输出打印项次(“经销商提款”+c);
经销商。点击(c);
c=myDeck.dealCard();
System.out.println(“经销商的总手数当前为“+Dealer.totalHand()+”);
如果(dealer.totalHand()==21)
{
System.out.println(“经销商获胜”);
系统出口(0);
}
否则如果(dealer.totalHand()>21)
{
System.out.println(“经销商破产,你赢了”);
我。报酬();
系统出口(0);
}
}
如果(me.totalHand()>dealer.totalHand())
System.out.println(“你赢了!”);
我。报酬();
如果(me.totalHand()
}


为了澄清我的问题,wagerBet()方法要求用户以下注的形式进行双重输入。如果玩家赢了一手牌,那么rewardBet()方法将奖励玩家,将其下注的金额加上奖励,因此“下注*2”。问题是rewardBet()方法根本无法识别“下注”输入,我正在尝试找出如何做到这一点。例如,我下注50美元,所以现在我有950美元(默认值为1000美元)。我赢得了这一轮,所以rewardBet()需要给我100美元。现在它没有给我任何获胜的机会。

好吧,一个问题是在你主要方法的最后一行:

if(me.totalHand() > dealer.totalHand())
    System.out.println("You win!");
    me.rewardBet();
如果只在print语句上运行,则需要将这个主体用大括号括起来,即if语句。尽管这似乎不能解决您描述的问题

也许你应该考虑做一个完全不同的设计,避免使用这么多的重复代码。 21点

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;
    }
}
公共类21点
{
私人甲板;
私人玩家我;
私人玩家经销商;
公共静态void main(字符串[]args)
{
21点游戏=新21点();
game.run();
}
公共21点()
{
甲板=新甲板();
洗牌();
me=新玩家(“乔”,1000.0);
经销商=新玩家(“经销商”,0);
}
公开募捐
{
双倍下注=请求下注(me);
//先发两张牌
dealNextCard(我说,“你的第一张卡是”);
dealNextCard(我说,“你的第二张卡是”);
me.printHandTotal();
//发第一张牌
dealNextCard(经销商,“经销商展示”);
while(requestHitOrStay())
{
dealNextCard(我说,“你的下一张牌是”);
me.printHandTotal();
if(me.totalHand()==21)
{
System.out.println(me.getName()+“wins!”);
奖赏赌注(我,打赌);
系统出口(0);
}
否则如果(me.totalHand()>21)
{
System.out.println(me.getName()+“bust!”);
系统出口(0);
}
}
而(dealer.totalHand()<17)
{
dealNextCard(经销商,“经销商提款”);
dealer.printHandTotal();
如果(dealer.totalHand()==21)
{
System.out.println(dealer.getName()+“wins!”);
系统出口(0);
}
否则如果(dealer.totalHand()>21)
{
System.out.println(dealer.getName()+“bust.youwin!”);
奖赏赌注(我,打赌);
系统出口(0);
}
}
如果(me.totalHand()>dealer.totalHand())
{
System.out.println(“你赢了!”
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;
    }
}