如何在我的21点半身像系统中修复此if语句



我刚开始为学校编写代码和作业。我正在用Java编写21点代码。这可能是一个简单的修复,但我的代码做这个


经销商总数为7

您的总为11

你想要另一张卡片吗?是/否y


您的总数是20你想要另一张卡片吗?是/否y


您的总数是26你想要另一张卡片吗?是/否y


您的总数是26你破产了你现在有400.0美元你想再玩一次吗?y/n

问题是它会高于21,但直到下一个循环才告诉你你破产了。我希望它只说最后一个,而不是倒数第二个和最后一个。

System.out.println("would you like another card? ny/n");
answer = sanswer.nextLine();
//if player answers
while ("y".equals(answer)) {
if (total < 21){
hit = (int) Math.floor(Math. random() * 10) + 1;
total += hit;
System.out.println("____________________________n");
System.out.println("Your total is " + total);
System.out.println("Would you like another card? ny/n");
answer = sanswer.nextLine();
}  
//player bust
if (total > 21) {
System.out.println("______________");
System.out.println("Your total is " + total);
System.out.println("You went bust");     
System.out.println("You now have $" + money);
play = "no";
System.out.println("Would you like to play again? y/n");
answer = "";
play = srplay.next();
}
}

考虑一下这个地方:

total += hit;

在这一点上,总数可能超过21。检查一下。根据价值行事:

if (total > 21) { /* do something here */ }

最新更新