Java BlackJack没有进入while循环

时间:2016-02-18 14:12:15

标签: java

首先,我道歉,因为这段代码对我来说似乎效率低下,但我的教授想要这样(评论是她的)。我在这里遇到很多问题,但一个中心问题似乎是当我运行程序时它不会进入for循环。因此我假设它没有进入for循环所在的while循环,我不知道为什么。这是“修改过的”二十一点程序,任何帮助表示赞赏:

import java.util.Scanner;
import java.util.Random;

public class BlackJack {
enum Decisions

{
    HIT, STAND, SURRENDER, QUIT, PLAY, NOTVALID;
    // NOTVALID is used to re-ask to play again
}

enum Card {
    ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, QUEEN, JACK, KING;
}

// Tells you if the Decision enum is valid or not
public static boolean containsDecision(String decision) {
    for (Decisions d : Decisions.values()) {
        if (d.name().equals(decision)) {
            return true;
        }
    }
    return false;
}

// Tells you what is the integer value of said Card enum
public static int getCardValue(int pick) {
    if (pick == 0) {
        return 1;
    } else if (pick >= 10 && pick <= 12) {
        return 10;
    }
    return pick + 1;
}

// 1 pts
public static int housePlay(int houseCardSum) {
    int cardPick;

    cardPick = -1;

    while (houseCardSum <= 17)
    {
        getCardValue(cardPick); 
        houseCardSum = (cardPick + houseCardSum);

        System.out.println("Hit from house! CARD: " + cardPick + " VALUE: " + houseCardSum);
    }   
    return houseCardSum;
}

// 1.5 pts
public static void findWinner(int playerCardSum, int houseCardSum, Decisions decision) {

        houseCardSum = housePlay(houseCardSum);

        if ( (playerCardSum > 21) || (houseCardSum > 21))
    {
        if (houseCardSum > 21) 
        {
            System.out.println("Sorry you lose!");
        }
        else 
        {
            System.out.println("You win!");
        }
    }
    else if (decision == Decisions.SURRENDER)
    {
        System.out.println("You lose because you surrendered!");
    }
    else if (playerCardSum == houseCardSum)
    {
        System.out.println("Its a push (tie)!");
    }
    else if ((playerCardSum > houseCardSum) && (playerCardSum < 21))
    {
        System.out.println("You win!");
    }
    else if ( (playerCardSum == 21) && (houseCardSum != 21) )
    {
        System.out.println("Blackjack win!");
    }
    else 
    {
        System.out.println("Sorry you lose!");
    }
    // if, else if, else, check playerCardSum, houseCardSum as well as if
    // Decision equaled to SURRENDER
    // print the House and You Hand at the end
    // call housePlay function and assign the output to houseCard function
    // iff not playerCardSum is not 21 and Decisions is not Surrender
    // these below will be used moved them around freely in this function
    // where you need too.
     // put in correct branch sub
                                            // statement
    System.out.println("House Hand: " + houseCardSum + " Your Hand: " + playerCardSum); // this
                                                                                        // is
                                                                                        // printed
                                                                                        // out
                                                                                        // after
                                                                                        // all
                                                                                        // branch
                                                                                        // statements
                                                                                        // assessed
}

// 7.5pts
public static void main(String[] args) {
    System.out.println("========== BlackJack ==========");
    System.out.print("What is your name: ");

    Scanner keyboard = new Scanner(System.in);

    String playerName;

    playerName = keyboard.nextLine();

    System.out.println("Okay, " + playerName + ", let's play!");

    Decisions decision = Decisions.PLAY ;

    int cardPick;

    cardPick = -1;

    while ( decision != Decisions.QUIT);
    {
        int playerSum;
        int houseSum;

        playerSum = 0;
        houseSum = 0;

        Card holeCard = null;


        for ( int i = 0; i < 3; i++)
            {

                Random newCard = new Random();

                cardPick = newCard.nextInt(14);


                playerSum = playerSum + getCardValue(cardPick);

                System.out.println("Card: " + (Card.values()[cardPick]) );  

                System.out.println("Sum of your hand: " + playerSum );              
            }
        }

1 个答案:

答案 0 :(得分:3)

在while语句后删除分号。

while ( decision != Decisions.QUIT);

while ( decision != Decisions.QUIT)