Java扑克计划

时间:2013-12-15 04:28:12

标签: java poker

我的代码遇到了问题。当我运行它时,程序继续运行,它不会停止。我不确定我做错了什么。我已经过去了,但我无法弄明白。有人请帮忙!

public void play() {
    /**
    * The main algorithm for single player poker game
    *
    * Steps: showPayoutTable()
    *
    * ++ show balance, get bet verify bet value, update balance reset deck,
    * shuffle deck, deal cards and display cards ask for position of cards
    * to keep get positions in one input line update cards check hands,
    * display proper messages update balance if there is a payout if
    * balance = O: end of program else ask if the player wants to play a
    * new game if the answer is "no" : end of program else :
    * showPayoutTable() if user wants to see it goto ++
    */
    // implement this method!

    Scanner input = new Scanner(System.in);
    List<Card> keepCard = new ArrayList<Card>();
    int counter = 0;
    boolean newGame = true;
    boolean rightBet = false;
    while (newGame) {
        oneDeck.shuffle();
        showPayoutTable();

        System.out.println("Balance:" + balance + "\n");

        while (!rightBet) {
            System.out.print("Enter bet:");
            bet = Integer.parseInt(input.nextLine());

            if (bet > balance) {
                System.out.println("insufficient fund!");
                rightBet = false;
            } else {
                rightBet = true;
            }
        }

        balance = balance - bet;

        try {
            currentHand = oneDeck.deal(5);
        } catch (PlayingCardException e) {
            System.out.println("Exception dealing a new hand" + e.getMessage());
        }

        System.out.println("" + currentHand.toString());
        System.out.print("Enter positions to keep:");

        if (input.hasNext()) {
            String s = input.nextLine();
            if (!(input.nextLine() == "0")) {
                input = new Scanner(s);
                input = input.useDelimiter("\\s*");

                while (input.hasNext()) {
                    keepCard.add(currentHand.get((input.nextInt()) - 1));
                    counter++;
                }
            }
        }
        currentHand = keepCard;
        try {
            currentHand.addAll(oneDeck.deal(5 - counter));
        } catch (PlayingCardException e) {
            System.out.println("Exception dealing the second hand" + e.getMessage());
        }

        System.out.println("" + currentHand.toString());
        checkHands();
        System.out.println("Your balance: " + balance + " you want another game y/n ?");

        if (input.hasNext()){
            String s = input.nextLine(); 
            if (balance == 0) {
                newGame = false;
                break;
            }

            if (s == "y") {
                newGame = true;
            } else {
                newGame = false;
            }

            System.out.println("Want to see payout table ? (y/n)");

            if (input.hasNext()) {
                s = input.nextLine();

                if (s == "y") {
                    showPayoutTable();
                }
                oneDeck.reset();

            }
            System.out.println("Bye!");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不要使用==比较字符串。请改用equals(...)equalsIgnoreCase(...)方法。理解==检查两个对象是否相同而不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,并且这才是最重要的。而不是

if (fu == "bar") {
  // do something
}

做,

if ("bar".equals(fu)) {
  // do something
}

,或者

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}