gpt4 book ai didi

java - 需要帮助从数组获取 int

转载 作者:行者123 更新时间:2023-11-30 04:07:11 24 4
gpt4 key购买 nike

我有一款 war 卡牌游戏。两名玩家各抽一张牌,然后相互比较,看看谁的手牌更大。我已经完成了大部分程序,但我不知道如何将卡值传递给 WarUI。每次我使用 get 方法时,它都会显示 1,所以我一定做错了什么。数组数字是 1-13 之间的 int 值,我使用 if 语句将 1 更改为 ace,11 更改为 jack 等。我在卡片类中使用 getValue() 时遇到问题。我该怎么办?

编辑:如何获取用户卡值以便我可以将它们相互比较?

import java.util.ArrayList;
import java.util.Random;

public class Card {
String finalCard = "";
int suit, number,number2;
static String [] suits = {"Heart" , "Diamond" , "Spade" , "Club"}; //suits
//static String [] numbers2 = { "Ace", "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King" }; //card values
static int [] numbers = { 1, 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 }; //card values
String card = "";

public Card() {

}
public Card(int suits, int numbers)
{
suit = suits;
number = numbers;

}


public int getValue() { // cant get this to work

int well = numbers[number];
return well;
}

public int getSuit() {
return suit;
}
public String toString()
{
String fakeValue = "";
if (numbers[number] == 1)
{
fakeValue = "Ace";
}
else if (numbers[number] == 11)
{
fakeValue = "Jack";
}
else if (numbers[number] == 12)
{
fakeValue = "Queen";
}
else if (numbers[number] == 13)
{
fakeValue = "King";
}
else

fakeValue = Integer.toString(numbers[number]);
String finalCard = fakeValue + " of " + suits[suit];
return finalCard;
}
}
import java.util.ArrayList;
import java.util.Random;

public class FullDeck {
private ArrayList<Card> cards = new ArrayList<Card>();//card array list
public FullDeck()
{
for(int a =0; a<=3; a++) //loops through suits
{
for(int b =0; b<=12;b++) //loops through values
{

cards.add(new Card(a,b)); //creates adds cards to list
}

}

}

public Card drawRandomCard()
{
Random generator = new Random(); //picks random card
int index = generator.nextInt(cards.size());

return cards.remove(index); //removes card from list
}

public String toString()
{
String result = "Cards remaining in deck: " + cards; //not currently used
return result;
}
}

import java.applet.Applet;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*;

public class WarUI extends JApplet
implements ActionListener {
JTextArea displayLabel = new JTextArea(""); //sets label to display message
JTextArea displayLabel2 = new JTextArea(""); //sets label to display message
JButton runButton = new JButton("Run"); //button that starts program
Container con = getContentPane(); //gets container


Card player1;
Card player2;
FullDeck hand1 = new FullDeck();

Card card = new Card();


public void init() {
con.setLayout(new FlowLayout());//sets flowlayout
con.add(new JLabel()); //jlabel container
con.add(runButton); //run button container
con.add(displayLabel); //display label container
con.add(displayLabel2); //display label container
runButton.addActionListener(this);//looks to see if run is clicked
}

public void actionPerformed(ActionEvent e) {


for (int i = 0; i < 1; i++) {
player1= hand1.drawRandomCard(); //draws cards for player 1
player1.toString();
}
for (int i = 0; i < 1; i++) {
player2= hand1.drawRandomCard(); //draws cards for player 2
player2.toString();
}

displayLabel.setText(player1.toString() + "\n" + player2.toString()+ card.getValue()); //displays both players values/suits

}
}

最佳答案

每当创建 Card 时,都会调用 Card() 构造函数。它将您的实例变量数值设置为 0。

public  int getValue() { // cant get this to work

int well = numbers[number];
return well;
}

在此方法中,数字 = 0。getValue() 返回数字[0],即 1。而是使用参数构造函数创建一张卡片。

public Card(int suits, int numbers)
{
suit = suits;
number = numbers;

}

示例:

Card card = new Card(2,2);

关于java - 需要帮助从数组获取 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20487885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com