所以事情就是这样。我开始研究简单的java编程。并达到了涵盖所有简单类(class)的程度,并且已经知道如何进行一些简单的编码。我遇到的麻烦是在一个代码中使用我所涵盖的所有知识。我想创建一个游戏,因为我被告知这是可能的,我认为他们是在 Java 的实际类(class)上做的。虽然我遇到了一些问题。它正在工作,但当玩家的手牌上升超过 24 点时,它不会停止。在游戏中,玩家应该在什么地方破产,它不断询问你是否想要另一张卡?我已经使用了 if 语句,但它似乎没有注意到它。
import java.util.Scanner;
public class Pontoon{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int playerScore = 0, computerScore = 0;
String newCard = "";
playerScore += dealCard();
computerScore += dealCard();
playerScore += dealCard();
computerScore += dealCard();
System.out.printf("Your score is %d would you like another card ? y/n ", playerScore);
newCard = input.nextLine();
if(newCard.equalsIgnoreCase("Y")) {
while (newCard.equalsIgnoreCase("Y") && playerScore < 21) {
playerScore += dealCard();
System.out.printf("Your score is %d would you like another card ? y/n ", playerScore);
newCard = input.nextLine();
}
}
while(computerScore < 15){
computerScore += dealCard();
}
checkWin(playerScore, computerScore);
}
public static int dealCard(){
int value = (int) (Math.random() * 13) + 1;
int score = 0;
if(value == 1) {
System.out.println("Ace!");
score = 11;
}else if (value == 13) {
System.out.println("King!");
score = 10;
}else if (value == 12) {
System.out.println("Queen!");
score = 10;
} else if (value == 11) {
System.out.println("Jack!");
score = 10;
} else {
score = value;
}
return score;
}
public static void checkWin(int Player, int Computer) {
int player = Player;
int computer = Computer;
if (player > 21) {
System.out.println("Player Bust");
} else if (computer > 21) {
System.out.println("Computer Bust");
} else {
if (player > computer) {
System.out.println("Player Wins!");
} else {
System.out.println("Computer Wins!");
}
}
}
}
最佳答案
在 while 循环中:
while (newCard.equalsIgnoreCase("Y")) {
添加一个条件来检查玩家得分
while (newCard.equalsIgnoreCase("Y") && playerScore < 21) {
关于java - 浮桥游戏迷失在代码中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8450343/