gpt4 book ai didi

java - 全局变量无法识别用户输入的数字

转载 作者:行者123 更新时间:2023-11-29 08:41:55 24 4
gpt4 key购买 nike

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。

最佳答案

首先让我们看看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。所以你将不得不用细齿梳理它。

您的一个基本问题是出于某种原因,您在前两张牌和所有牌上到处使用不同的变量。如果你只使用一个 aiTotaltotal边走边累积卡片总数,可以避免大多数此类问题。

关于java - 全局变量无法识别用户输入的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39502583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com