このループに何か問題があります (私はそう思います)。ループが実行されると、変数 userHandValue に保存されているデータがクリアされます。私はそれを回避するためにさまざまなループを試しましたが、引いた新しいカードが手札に追加されていないことを除いて、すべて問題ないと思います. プログラムを実行すると、私が話していることがわかります。手札の合計が 13 になることから始めて、ヒットすると手札の合計が 10 になることがあります。
私はこれに慣れていないので、フォーマットがあなたの好みに合わない場合は、私の投稿を閉じるのではなく、提案を提供してください (実際の問題を解決するのも素晴らしいでしょう)。ありがとうございました。
do {
if (userHandValue < 21) {
System.out.println("Would you like to hit? (Y/N)");
hit = dataIn.readLine();
hit = hit.toUpperCase();
}
again = hit.compareTo("Y") == 0;
if (again) {
// users next card draw
randomGenNum = (int)((range * Math.random()) + 1)*2;
// assigning the user hand value & card, pulling index from array
tempUserHandValue = arrayCardValues[randomGenNum];
// Check if the user was dealt an ace
if ((tempUserHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) {
tempUserHandValue = userHandValue + 10;
}
userCard = arrayCardSuites[randomGenNum];
// displaying values to the screen
System.out.println("Your next card is " + userCard);
// adding new card to total hand value and displaying to user
userHandValue = userHandValue + tempUserHandValue;
System.out.println("Your new total hand value is: " + (userHandValue));
}
} while (again);
ここにすべてがあります:
/*
*This is a simple BlackJack game written by C. Auito and D. Harrington for CS241.
*Code snippets were taken from the following open source files:
* http://www.dreamincode.net/forums/topic/75554-blackjack-game/
* update test
*
*/
package blackjackclassproject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author C. Auito <chris.auito@gmail.com>
*/
public class BlackJackClassProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
//Declare Variables
boolean again = true;
//int again = 0;
do {
double range = 26;
int randomGenNum;
int kitty = 1000;
int yourBet;
int userHandValue = 0;
int userDrawnValue;
int dealerHandValue = 0;
int dealerDrawnValue;
String playAgain;
String hit = "";
String strBetAmount;
String userCard, dealerCard;
int[] arrayCardValues = {0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6,
7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
String[] arrayCardSuites = {
"", "", "Ace/Clubs", "Ace/Diamonds", "Ace/Hearts", "Ace/Spades",
"2/Clubs", "2/Diamonds", "2/Hearts", "2/Spades", "3/Clubs", "3/Diamonds", "3/Hearts", "3/Spades",
"4/Clubs", "4/Diamonds", "4/Hearts", "4/Spades", "5/Clubs", "5/Diamonds", "5/Hearts", "5/Spades",
"6/Clubs", "6/Diamonds", "6/Hearts", "6/Spades", "7/Clubs", "7/Diamonds", "7/Hearts", "7/Spades",
"8/Clubs", "8/Diamonds", "8/Hearts", "8/Spades", "9/Clubs", "9/Diamonds", "9/Hearts", "9/Spades",
"10/Clubs", "10/Diamonds", "10/Hearts", "10/Spades", "Jack/Clubs", "Jack/Diamonds", "Jack/Hearts",
"Jack/Spades", "Queen/Clubs", "Queen/Diamonds", "Queen/Hearts", "Queen/Spades", "King/Clubs",
"King/Diamonds", "King/Hearts", "King/Spades"
};
//get bet amount from user
System.out.println("How much do you want to bet?");
strBetAmount = dataIn.readLine();
yourBet = Integer.parseInt(strBetAmount);
//draw three cards (one card face up for dealer, two for player)
//dealer first drawn card
randomGenNum = (int) ((range * Math.random()) + 1) * 2;
//assigning the dealer hand value & card, pulling index from array
dealerHandValue = arrayCardValues[randomGenNum];
//Check if the dealer was just dealt an ace
if ((randomGenNum >= 2) && (randomGenNum <= 5)) {
dealerHandValue = dealerHandValue + 10;
}
int tempDealerHandValue = dealerHandValue;
dealerCard = arrayCardSuites[randomGenNum];
//displaying values to the screen
System.out.println("The dealer is showing a " + dealerCard);
//dealers second drawn card
randomGenNum = (int) ((range * Math.random()) + 1) * 2;
//assigning the dealer hand value & card, pulling index from array
dealerHandValue = arrayCardValues[randomGenNum];
//Check if the dealer got an ace
if ((dealerHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) {
dealerHandValue = dealerHandValue + 10;
}
dealerCard = arrayCardSuites[randomGenNum];
//users first drawn card
randomGenNum = (int) ((range * Math.random()) + 1) * 2;
//assigning the user hand value & card, pulling index from array
userHandValue = arrayCardValues[randomGenNum];
//Check if the user was just dealt an ace
if ((randomGenNum >= 2) && (randomGenNum <= 5)) {
userHandValue = userHandValue + 10;
}
int tempUserHandValue = userHandValue;
userCard = arrayCardSuites[randomGenNum];
//displaying values to the screen
System.out.println("Your first card is " + userCard);
//users second drawn card
randomGenNum = (int) ((range * Math.random()) + 1) * 2;
//assigning the user hand value & card, pulling index from array
userHandValue = arrayCardValues[randomGenNum];
//Check if the user got an ace
if ((userHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) {
userHandValue = userHandValue + 10;
}
userCard = arrayCardSuites[randomGenNum];
//displaying values to the screen
System.out.println("Your second card is " + userCard);
System.out.println("Your total hand value is: " + (tempUserHandValue + userHandValue));
//check for the either player having a blackjack(hand value = 21)
if ((dealerHandValue == 21) && (userHandValue != 21)) {
//dealer = 21 user != 21 dealer wins, kitty - bet, game over
}
if ((dealerHandValue != 21) && (userHandValue == 21)) {
//user = 21 dealer != user wins, kitty + bet, game over
}
if ((dealerHandValue == 21) && (userHandValue == 21)) {
//dealer and user = 21 noone wins, users kitty is not changed, game over
}
//if neither player has 21 continue through the loop
//start the users loop
//while(userHandValue < 21)
if (userHandValue < 21) {
System.out.println("Would you like to hit? (Y/N)");
hit = dataIn.readLine();
hit = hit.toUpperCase();
}
if (hit.compareTo("Y") == 0) {
again = true;
} else {
again = false;
}
if ((again) == (true)) {
//users next card draw
randomGenNum = (int) ((range * Math.random()) + 1) * 2;
//assigning the user hand value & card, pulling index from array
tempUserHandValue = arrayCardValues[randomGenNum];
//Check if the user was dealt an ace
if ((tempUserHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) {
tempUserHandValue = userHandValue + 10;
}
userCard = arrayCardSuites[randomGenNum];
//displaying values to the screen
System.out.println("Your next card is " + userCard);
//adding new card to total hand value and displaying to user
userHandValue = userHandValue + tempUserHandValue;
System.out.println("Your new total hand value is: " + (userHandValue));
}
} while (again == true);