import java.util.*;
//Hope you enjoy playing Blackjack!
public class Blackjack {
public static void main(String[] args) {
System.out.println("//**BLACKJACK**//");
Random r = new Random(); // generates cards
Scanner sc = new Scanner(System.in);
String replay;
double dBal = 100.60; // fixed balance of the dealer
double dBet = 1.0 + r.nextDouble() * 100.60;
System.out.println("Enter your balance:"); // incorporating betting
double pBal = sc.nextDouble();
do {
System.out.println("Enter amount you wish to bet upon:");
double pBet = sc.nextDouble();
if (pBet > pBal) { // you can't bet more than your balance ;)
System.out.println("BLACKJACK error");
System.exit(0);
}
System.out.println("You have chosen to bet: " + pBet);
System.out.println("Dealer wishes to bet :" + dBet);
pBal -= pBet;
dBal -= dBet;
int p1 = 1 + r.nextInt(11); // player card #1
int p2 = 1 + r.nextInt(11); // player card #2
int p3 = 1 + r.nextInt(11); // player card #3
int playerTotal = p1 + p2;
System.out.println("You drew: " + p1 + " and " + p2 + ". Your total is: " + playerTotal);
int d1 = 1 + r.nextInt(11); // dealer card #1
int d2 = 1 + r.nextInt(11); // dealer card #2
int d3 = 1 + r.nextInt(11); // dealer card #3
int dealerTotal = d1 + d2;
System.out.println("Dealer drew: " + d1 + ". Would you like to hit
or stay?"); // [player] choice to 'hit' or 'stay'
String playerChoice = sc.nextLine(); // this is the problem area
if (playerChoice.equals("hit")) { // 'hit' condition
playerTotal = p1 + p2 + p3;
System.out.println("You drew: " + p3 + ". Your total is: " +
playerTotal);
} else if (playerChoice.equals("stay")) { // 'stay' condition
System.out.println("Player stays.");
}
System.out.println("Would dealer like to hit or stay?");
int key = 1 + r.nextInt(2); // random choice to decide whether
dealer 'hits' or 'stays'
switch (key) {
case 1:
dealerTotal = d1 + d2 + d3;
System.out.println("Dealer hits. Dealer had " + d2 + ". Dealer
drew " + d3 + ". Dealer's total is: "
+ dealerTotal);
break;
case 2:
System.out.println("Dealer stays. Dealer had " + d2 + ".
Dealer's total is: " + dealerTotal);
break;
default:
System.out.println("BLACKJACK error");
}
if (playerTotal > dealerTotal && playerTotal < 21) { // deciding
who has won
System.out.println("Player wins.");
pBal += pBet += dBet;
} else if (dealerTotal >= playerTotal && dealerTotal < 21) {
System.out.println("Dealer wins.");
dBal += dBet += pBet;
} else if (p1 + p2 + p3 >= 21) {
System.out.println("Player has busted. Dealer wins.");
dBal += dBet += pBet;
} else if (d1 + d2 + d3 >= 21) {
System.out.println("Dealer has busted. Player wins.");
pBal += pBet += dBet;
} else {
System.out.println("BLACKJACK error.");
}
System.out.println("You have " +pBal+ " remaining. Do you want to
continue? (Y/N)");
replay = sc.nextLine();
}while (! replay.equals("N"));
}
}
// Author: Saahil Karnik
我编写了一个Java程序,可以让你在计算机上玩二十一点。然而,在第36和37行,该程序不允许我输入我是否要“点击”或“停留”。我已经声明了一个Scanner对象并且也使用过它,但是不扫描该字符串。有人可以帮我这个吗?