这是我的整个代码。如果总数超过21,我似乎无法找到一种方法来挑选A并将它们变成1。 / ******************* ************************************************** ************************************************** ****************
Name:
BlackJack_Mason_Haddock
Description:
The famous known game "BlackJack" created in Java.
History:
Nov 16 2016 / M. Haddock / Created: Code skeleton
Nov 17 2016 / M. Haddock / Created: Random number generator from 1 to 10
*********************************************************************************************************************************************************************/
import java.awt.*; // Used in graphics
import java.awt.event.*; // For mouse stuff
import java.applet.*; // For mouse/music stuff
import java.io.*; // For input and output
import java.util.*; // For date and time stuff
import java.lang.*; // For point stuff
import hsa.Console; // Special console input output commands
class BlackJack_Mason_Haddock
{
public static void Title (Console c)
{
}
public static void Game (Console c)
{
int givenNumber[] = new int [52]; // Makes an empty array of integers of 52. Used for 52 cards in the deck
int i; // Used for any for loops of drawing cards
int total = 0; // Sets a base number for the total number of card scores
char next; // Used to catch the first letter of the string "string"
String string; // Used for a string given by the user
int ace = 11; // Setting aces to 11
int ace2 = 1; // Setting aces to 1 if 11 is over 21
for (i = 0 ; i < 1 ; i++) // Runs through the code below twice, once at a time.
{
givenNumber [i] = (int) (Math.random () * (11 - 1)) + 1; // Picks a random number from 1 to 10
c.print (givenNumber [i] + " "); // Outputs the random number picked above, adds a space for the second number
total = total + givenNumber [i];
if ((total < 22) && (givenNumber [i] == 1))
{
givenNumber [i] = ace;
total = total + 10;
}
if ((total > 21) && (givenNumber [i] == ace))
{
givenNumber [i] = ace2;
}
givenNumber [i] = (int) (Math.random () * (11 - 1)) + 1; // Picks a random number from 1 to 10
c.print (givenNumber [i] + " "); // Outputs the random number picked above, adds a space for the second number
total = total + givenNumber [i];
for (i = 0 ; i < 51 ; i++)
{
if ((total < 22) && (givenNumber [i] == 1))
{
givenNumber [i] = ace;
total = total + 10;
}
}
for (i = 0 ; i < 51 ; i++)
{
if ((total > 21) && (givenNumber [i] == ace))
{
givenNumber [i] = ace2;
}
}
if (total == 21)
{
c.print ("BlackJack! You Win!");
return;
}
do
{
c.println ("");
c.print ("Will you hit or stand?");
c.println ("");
string = c.readString ();
next = string.charAt (0);
c.println ("");
if ((total < 21) && (next == 'S') || (next == 's'))
{
c.print ("You stand at " + total);
return;
}
else if ((total < 21) && (next == 'H') || (next == 'h'))
{
givenNumber [i] = (int) (Math.random () * (11 - 1)) + 1;
c.print (givenNumber [i] + " ");
for (i = 0 ; i < 51 ; i++)
{
if ((total > 22) && (givenNumber [i] == 11))
{
total = total - 10;
}
}
total = total + givenNumber [i];
c.println ("");
c.print ("Total " + total + " ");
if (total == 21)
{
c.print ("You Win!");
return;
}
if (total > 21)
{
c.println ("");
c.print ("You bust!");
return;
}
} //End of the if statement
} //End of the for statement
while (true);
}
}
public static void main (String[] args)
{
Console c = new Console (32, 167, "BlackJack");
Title (c);
Game (c);
}
}
答案 0 :(得分:1)
简短的回答是停止将你的11改为1并且只包括如何添加11的逻辑,特别是因为你已经有了他们的if语句。仅仅因为值为11并不意味着你必须添加11
编辑:事实上,鉴于你正在尝试重新制作二十一点,你没有理由在你的游戏中同时拥有11和1,因为Aces是唯一具有任何价值的牌。您可以非常轻松地从代码中删除11个
答案 1 :(得分:0)
正如托什所说,不是从总数中减去,而是有一段类似于
的代码if (draws an ace) {
if (total + 11 > 21) {
total++;
else {
total += 11;
}
}