gpt4 book ai didi

Java - 我正在编写我的第一个项目。这是二十一点,但我遇到了一些麻烦

转载 作者:行者123 更新时间:2023-11-30 03:46:07 26 4
gpt4 key购买 nike

我是编程新手。我试图玩二十一点,但遇到了一些问题。

我的控制台输出:

Would you like to play again? y/n 
y
You have: $50
Whats your bet:
12
You get a 7 and a 10
Your total is 17
The dealer has a 9 11 showing
Would you like to hit? y/n :
n
The dealer has 20 //Here it is supposed to say You lost blah blah blah
Would you like to play again? y/n //but its skipping to System.println("play again");
<小时/>
package loops;
import java.util.Scanner;
import java.util.Random;
public class loops {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
char hit = 0;
char playAgain = 0;
String dolphin = "banana";
int c = 5;
int win = 0;
int[] cards = {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,11,11,11,11};
int bet, money = 999999999, cardValue = 0, dealersValue = 0;
if(money == 999999999){
System.out.println("How much money would you like to start with?");
money = reader.nextInt();
}


do {
shuffleArray(cards);
System.out.println("You have: $" + money);
System.out.println("Whats your bet: ");
bet = reader.nextInt();
if(bet > money){
System.out.println("You cannot bet over " + money + "!");
System.exit(0);
break;
}


System.out.println("You get a " + cards[0] + " and a " + cards[1]);
cardValue = cards[0] + cards[1];
System.out.println("Your total is " + cardValue);

if(cardValue == 21){
win = 1;
}


if(cardValue > 21){
win = 2;
}


if(win != 2){

int i = 9;
System.out.println("The dealer has a " + cards[7] + " " + cards[8] + " showing");
dealersValue = cards[7] + cards[8];
System.out.println("Would you like to hit? y/n : ");
hit = reader.next().charAt(0);
while(dealersValue <= 17){
if(dealersValue <= 17){
dealersValue = cards[7] + cards[8] + cards[i];
}


if(dealersValue <= 17){
dealersValue = cards[7] + cards[8] + cards[i] + cards[i];
}


if(dealersValue <= 17){
dealersValue = cards[7] + cards[8] + cards[i] + cards[i] + cards[i];
}

if(cards[i] > 11 && dealersValue > 21){
cards[i] = 1;
}
if(cards[7] > 11 && dealersValue > 21){
cards[7] = 1;
}
if(cards[8] > 11 && dealersValue > 21){
cards[8] = 1;
}

i++;
}}


while(hit == 'y'){
System.out.println("You get a " + cards[c]);
cardValue = cardValue + cards[c];
System.out.println("Your total is " + cardValue);
if(cardValue > 21){
if(cards[5] > 11 && cardValue > 21){
cards[5] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}

if(cards[6] > 11 && cardValue > 21){
cards[6] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}

if(cards[7] > 11 && cardValue > 21){
cards[7] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}

if(cards[8] > 11 && cardValue > 21){
cards[8] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}

if(cards[9] > 11 && cardValue > 21){
cards[9] = 1;
System.out.println("To avoid busting, you 11 changes to 1");
}

if(cardValue > 21){
break;
}
}
System.out.println("Would you like to hit? y/n : ");
hit = reader.next().charAt(0);
c++;
if(hit =='n'){
System.out.println("You stay!");
cardValue = cardValue + 0;
break;
}
}
System.out.println("The dealer has " + dealersValue);

if(dealersValue > 21){
win = 1;
}
if(cardValue > 21){
win = 2;
}
if(cardValue == 21){
win = 1;
}
if(dealersValue == 21){
win = 2;
}
if (dealersValue > cardValue && dealersValue > 21){
win = 2;
}
if (dealersValue == cardValue){
win = 2;
}
if(dealersValue < cardValue && cardValue <=21) {
win = 1;
}
if(dealersValue == 21 && dealersValue != cardValue || dealersValue == 21 && dealersValue == cardValue || cardValue > 21){
win = 2;
}
if(cardValue == 21 && dealersValue != cardValue || dealersValue > 21){
win = 1;
}
if(win == 1){
System.out.println("You won "+bet*1 + " dollars!");;
System.out.println("Your total balance is " + money);
}
if(win == 2){
System.out.println("You lost "+bet*1 + " dollars!");;
System.out.println("Your total balance is " + money);
}
if(win == 3){
System.out.println("You lost "+bet*1 + " dollars!");;
System.out.println("Your total balance is " + money);
}
if(money <= 0){
System.out.println("You are out of money!");
System.exit(0);
}
System.out.println("Would you like to play again? y/n ");
playAgain = reader.next().charAt(0);

if(playAgain == 'n'){
System.exit(0); }
win = 0;

} while (playAgain == 'y');
}


static void shuffleArray(int[] cards){
Random rnd = new Random();
for (int i = cards.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
int a = cards[index];
cards[index] = cards[i];
cards[i] = a;
}
}
}

我多次梳理了代码,寻找出错的地方,比如可能是不应该出现的 } ,但我找不到任何东西。我认为它需要一些新鲜的眼光!在我让这段代码工作后,我计划使用面向对象的方法再次尝试。

最佳答案

设置变量“win”的 if 语句集不正确。

使用给定的示例浏览代码(庄家有 20 个,玩家有 17 个),变量“win”从未设置,因此所有打印出“你输了”和“你赢了”的语句都被跳过。

很有可能在这一行

if (dealersValue > cardValue && dealersValue > 21){

你的意思是

dealersValue < 21

这将导致 win 设置为 2 并显示正确的输出。

调试提示:使用 IDE 浏览代码。如果您逐行执行,您应该立即看到它跳过设置“win”变量。

祝学习编程一切顺利!这很有趣,但很令人沮丧:)

关于Java - 我正在编写我的第一个项目。这是二十一点,但我遇到了一些麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25655423/

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