我正在做一个卡片对游戏,它从字符串数组中生成两个随机值。我想知道的是如何从字符串数组中获取两个随机值的总和以确定获胜者。这是代码
import java.util.*;
public class Cards {
private String suit;
private String face;
private String[] cardSuits;
private String[] cardFaces;
private Random ran;
public Cards() {
ran = new Random();
cardSuits = new String[] { "of Spade", "of Hearts", "of Diamonds",
"of Clubs" };
cardFaces = new String[] { "Ace", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King" };
}
public String setPlayerCardSuit() {
suit = cardSuits[ran.nextInt(4)];
return suit;
}
public String setPlayerCardFace() {
face = cardFaces[ran.nextInt(13)];
return face;
}
public String setPlayerCardSuit2() {
suit = cardSuits[ran.nextInt(4)];
return suit;
}
public String setPlayerCardFace2() {
face = cardFaces[ran.nextInt(13)];
return face;
}
public String setCompCardSuit() {
suit = cardSuits[ran.nextInt(4)];
return suit;
}
public String setCompCardFace() {
face = cardFaces[ran.nextInt(13)];
return face;
}
public String setCompCardSuit2() {
suit = cardSuits[ran.nextInt(4)];
return suit;
}
public String setCompCardFace2() {
face = cardFaces[ran.nextInt(13)];
return face;
}
public void getResults() {
System.out.println("Here are your cards: " + setPlayerCardFace() + " "
+ setPlayerCardSuit() + " and " + setPlayerCardFace2() + " "
+ setPlayerCardSuit2());
}
public void getCompCard() {
System.out.println("Here's the computer's cards: " + setCompCardFace()
+ " " + setCompCardSuit() + " and " + setCompCardFace2() + " "
+ setCompCardSuit2());
}
}
这是测试 Cards 类的代码:
import javax.swing.JOptionPane;
public class TestCards {
public static void main(String[] args) {
Cards playerCards = new Cards();
Cards computerCards = new Cards();
int confirm, x = 1;
while (x == 1) {
JOptionPane.showMessageDialog(null,
"Random Card game \nPlease press OK to Start Game",
"Card Pair Game", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(
null,
"Here are your Cards: " + playerCards.setPlayerCardFace()
+ " " + playerCards.setPlayerCardSuit() + " and "
+ playerCards.setPlayerCardFace2() + " "
+ playerCards.setPlayerCardSuit2()
+ "\nThe Computer's Cards are: "
+ computerCards.setCompCardFace() + " "
+ computerCards.setCompCardSuit() + " and "
+ computerCards.setCompCardFace2() + " "
+ computerCards.setCompCardSuit2());
confirm = JOptionPane.showConfirmDialog(null, "Game Ends. Again?",
"Game Over", JOptionPane.YES_NO_OPTION);
if (confirm != JOptionPane.YES_OPTION) {
x = 2;
}
}
}
}
现在缺少的是确定获胜者的代码。
PS:我是Java编程的初学者..所以如果你看到不寻常的代码使用请多多包涵:)
我已经尝试过 Dylan 的建议,但我似乎无法让它发挥作用.. 而是使用了他的想法并将此代码添加到 Cards 类。
public int playerValues(){
int temp = 0;
if(face != cardFaces[0] && face != cardFaces[10] && face != cardFaces[11] && face != cardFaces[12]){
temp = Integer.parseInt(face);
}else if(face == cardFaces[0]){
temp = 1;
}else if(face == cardFaces[10]){
temp = 11;
}else if(face == cardFaces[11]){
temp = 12;
}else if(face == cardFaces[12]){
temp = 13;
}
return temp;
}
public int computerValues(){
int temp = 0;
if(face != cardFaces[0] && face != cardFaces[10] && face != cardFaces[11] && face != cardFaces[12]){
temp = Integer.parseInt(face);
}else if(face == cardFaces[0]){
temp = 1;
}else if(face == cardFaces[10]){
temp = 11;
}else if(face == cardFaces[11]){
temp = 12;
}else if(face == cardFaces[12]){
temp = 13;
}
return temp;
}