我到目前为止编写的代码在下面并且有效,但是我遇到了一些问题 1)它不止一次地运行程序 2)数字似乎不是很随机,它似乎选择相同的数字 3)我需要值1显示为Ace,值11显示为Jack,值12显示为Queen,值13显示为King
我已经多次查看了我的代码,无法弄清楚它为什么要做第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 + " ");
}
}
}
}
}
答案 0 :(得分:1)
您可以创建一个cardPlayers和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("");
}
这将使您的代码更具可读性并减少总计