import java.util.Scanner;
public class Blackjack {
class Commands {
static final String yes = "yes";
static final String no = "no";
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Opponent player2 = new Opponent();
double AiCard1 = 0;
double AiCard2 = 0;
double AiCard3 = 0;
double AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
double card1 = 0;
double card2 = 0;
double card3 = 0;
double card4 = 0;
double total2 = card1 + card2 + card3 + card4;
double total = card1 + card2 + card3;
System.out.println("Hello and Welcome to my custom version of blackjack!");
System.out.println("You will start off with $300");
System.out.println();
System.out.println("Do you want to read the rules before playing?");
System.out.print("Press 1 if yes, press 2 for no");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("You have 4 cards to get as close to 21 as possible. Whoever is closest to 21 wins");
break;
default:
break;
}
int balance = 300;
System.out.println("Your bank balance is $" + balance);
//user places a bet
System.out.println("Place a bet");
int bet = input.nextInt();
System.out.println("You placed a bet of " + bet);
//this is the AI
AiCard1 = Math.random() * 12 + 1;
AiCard1 = Math.round(AiCard1);
AiCard2 = Math.random() + 12 + 1;
AiCard2 = Math.round(AiCard2);
double AiTotal2cards = AiCard1 + AiCard2;
if(AiTotal2cards < 15) {
AiCard3 = Math.random() * 12 + 1;
AiCard3 = Math.round(AiCard3);
AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
}
card1 = Math.random() * 12 + 1;
card1 = Math.round(card1);
System.out.println("Your first card was a " + card1);
System.out.println();
card2 = Math.random() * 10 + 1;
card2 = Math.round(card2);
System.out.println("Your second card was a " + card2);
System.out.println();
System.out.println("Your cards add up to " + (card1 + card2));
System.out.println("Do you want to add another card?");
String answer = input.next();
here:
if(answer.equals(Commands.yes)) {
card3 = Math.random() * 12 + 1;
card3 = Math.round(card3);
System.out.println("Your third card was a " + card3);
System.out.println("Your cards add up to " + (card1 + card2 + card3));
total = card1 + card2 + card3;
if(total > 21) {
System.out.println("You lose");
break here;
}
System.out.println("Do you want to add another card?");
String answer1 = input.next();
if(answer1.equals(Commands.no)){
break here;
}
if(answer1.equals(Commands.yes)) {
card4 = Math.random() * 12 + 1;
card4 = Math.round(card4);
System.out.println("Your fourth card was a " + card4);
System.out.println("Your cards add up to " + (card1 + card2 + card3 + card4));
total2 = card1 + card2 + card3 + card4;
if(total2 > 21) {
System.out.println("You lose");
break here;
}
break here;
}
}
System.out.println("Your total cards were " + total2);
System.out.println("AI total cards were " + AiTotalcard3);
input.close();
}
}
当你运行程序时,我希望当用户通过程序时,顶部的变量会将新数字保存到它们中。有没有更好的方法来执行此操作,因为控制台输出始终为0。
答案 0 :(得分:2)
首先让我们看一下AiTotalcard3
:
它不应该总是输出0,更准确地说,它会在AiCard1 + AiCard2 >= 15
的情况下输出0,这可能会发生很多。
如果您追踪实际代码,这是有道理的。首先发生的是那些初始化器:
double AiCard1 = 0;
double AiCard2 = 0;
double AiCard3 = 0;
double AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
因此AiTotalcard3
最初设置为0.在您更改之前它将保持为0,这在此处发生:
AiCard1 = Math.random() * 12 + 1;
AiCard1 = Math.round(AiCard1);
AiCard2 = Math.random() + 12 + 1;
AiCard2 = Math.round(AiCard2);
double AiTotal2cards = AiCard1 + AiCard2;
if(AiTotal2cards < 15) {
AiCard3 = Math.random() * 12 + 1;
AiCard3 = Math.round(AiCard3);
AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
}
但只有当前两张卡加起来少于15时才会发生这种情况。之后你再也不会触碰AiTotalcard3
了。
您的逻辑可以极大地简化,并且使用int
而不是double
来表示更有意义,但忽略所有这些和坚持你当前的风格,你可以通过确保所有路径设置AiTotalcard3
来解决这个问题(例如,再次不是最干净的逻辑,只是坚持你的模式):
if(AiTotal2cards < 15) {
AiCard3 = Math.random() * 12 + 1;
AiCard3 = Math.round(AiCard3);
AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
} else {
AiTotalcard3 = AiCard1 + AiCard2; // be sure to always set it
}
现在,考虑到所有这些,total2
存在类似问题:并非所有执行路径都设置了total2
的值。如果你有效使它达到第4张牌,但在此之前你永远不会从初始值0改变它。所以你只需要用细齿梳子来完成它。
您的一个基本问题是由于某些原因,您在前2张牌与所有牌中使用不同的变量。如果您只是使用一个aiTotal
和total
来累积卡总数,那么您就可以避免大部分问题。
答案 1 :(得分:2)
我认为您的主要问题是total
和total2
混淆。你好像在多个地方都可以互换。
首先,如果用户没有选择第三张卡片,total2
将始终等于0,因为如果从未输入if(answer.equals(Commands.yes)) {
,则total2
永远不会设置为原始初始化0
如果用户选择第三张卡但不是第四张卡,则会正确更新total
但不会total2
的值。
如果用户选择了第四张卡,那么它应该可以正常工作。
除了主要修正之外,这里还有一些在编写游戏时应该记住的提示:
无论如何,祝你好运!编程起初可能令人生畏,但这是一个很好的开始。