到目前为止我编写的代码在下面并且可以工作,但是我遇到了一些问题1(它运行程序不止一次2(数字似乎不是很随机,它似乎挑出相同的数字3( 我需要值 1 显示为 Ace,值 11 显示为杰克,值 12 显示在女王,值 13 显示为国王
我已经多次检查了我的代码,但无法弄清楚为什么它会在第 1 点做我不确定是只有我一个人,还是第 2 点的问题我知道我需要一个 if 语句来执行第 3 个,但不太确定将其放在哪里
该程序的想法是让玩家在计算机上依次抽出 7 张牌(不需要包括套件(并输出每张牌,然后输出获胜者,其中 1 张输出为 ace,11 输出为千斤顶,12 输出为女王,13 输出为国王。
非常感谢您的帮助
import java.math.*;
import java.util.*;
public class CardProj {
public static void main(String[] args) {
Scanner mykbd = new Scanner(System.in);
Random rn = new Random();
{
for (int i = 0; i < 14; i++) {
int card1player = 0;
int card2player = 0;
int card3player = 0;
int card4player = 0;
int card5player = 0;
int card6player = 0;
int card7player = 0;
int card1Computer = 0;
int card2Computer = 0;
int card3Computer = 0;
int card4Computer = 0;
int card5Computer = 0;
int card6Computer = 0;
int card7Computer = 0;
System.out.println("Player Card 1: " + card1player);
card1Computer = rn.nextInt(7) + 1;
System.out.println("Computer Card 1: " + card1Computer);
System.out.println("");
card2player = rn.nextInt(7) + 1;
System.out.println("Player Card 2: " + card2player);
card2Computer = rn.nextInt(7) + 1;
System.out.println("Computer Card 2: " + card2Computer);
System.out.println("");
card3player = rn.nextInt(7) + 1;
System.out.println("Player Card 3: " + card3player);
card3Computer = rn.nextInt(7) + 1;
System.out.println("Computer Card 3: " + card3Computer);
System.out.println("");
card4player = rn.nextInt(7) + 1;
System.out.println("Player Card 4: " + card4player);
card4Computer = rn.nextInt(7) + 1;
System.out.println("Computer Card 4: " + card4Computer);
System.out.println("");
card5player = rn.nextInt(7) + 1;
System.out.println("Player Card 5: " + card5player);
card5Computer = rn.nextInt(7) + 1;
System.out.println("Computer Card 5: " + card5Computer);
System.out.println("");
card6player = rn.nextInt(7) + 1;
System.out.println("Player Card 6: " + card6player);
card6Computer = rn.nextInt(7) + 1;
System.out.println("Computer Card 6: " + card6Computer);
System.out.println("");
card7player = rn.nextInt(7) + 1;
System.out.println("Player Card 7: " + card7player);
card7Computer = rn.nextInt(7) + 1;
System.out.println("Computer Card 7: " + card7Computer);
System.out.println("");
int playertotal = card1player + card2player + card3player + card4player + card5player + card6player + card7player;
int computertotal = card1Computer + card2Computer + card3Computer + card4Computer + card5Computer + card6Computer + card7Computer;
if (playertotal > computertotal) {
System.out.print("Player Wins, Players Score is, " + playertotal + " Computers Score is, " + computertotal + " ");
} else if (computertotal > playertotal) {
System.out.print("Computer Wins, Computer Score is, " + computertotal + " Players Score is, " + playertotal + " ");
} else {
System.out.print("Draw Players Score is, " + playertotal + " Computers Score is, " + computertotal + " ");
}
}
}
}
}
您可以创建 cardPlayer 和 cardComputers 的数组。例如,在下面尝试这个
int numPlayers = 14;
int [] cardPlayer = new int[numPlayers];
int [] cardComputer = new int[numPlayers];
for (int i = 0; i < numPlayers; i++) {
System.out.println("Player Card " + i + ": " + cardPlayer[i]);
cardComputer[i] = rn.nextInt(7) + 1;
System.out.println("Computer Card " + "i" + cardComputer[i]);
System.out.println("");
}
这将使您的代码更具可读性并减少总行数