使用类和对象"War"纸牌游戏生成器问题 (Java 7)



我今天的问题是,如何为状态变量/方法分配值,以比较值以确定获胜者?另外,如何将值引导到"杰克"one_answers"吉尔"(播放器P1和P2)?

我应该使用if-else语句吗?

和最后一个问题:为什么我的控制台为我的getCore,getcarduit,getCardValue等打印" null"?(这一切之后,我会给您我的控制台打印输出)

这是我的"玩家类"的代码,我的"测试器"项目在此代码下方(测试我的播放器类):

public class player {

// The two players are going to enter:
String p1[] = {"Jack", "h", "k"};
String p2[] = {"Jill", "d", "9"};
//Setting up values
String Jack = "Jack";
String Jill = "Jill";
String h = " Hearts ";
String d = " Diamonds ";
String k = " King ";
int val = 9;
// Score
public int score = 0; // State variable score, set equal to 0

// Player name - Jack, Jill
public player(String Jack, String h, String k) {
    // TODO Auto-generated constructor stub
}

public String playerName(String player)
{
    player = "Jack";
    player = "Jill";
    return player;
}

// Card suit
public String cardSuit(String getcardSuit)
{
    return cardSuit;
}
// Card Value for player 1

public String getCardValue() 
{
    return cardValue;
}
public String getScore(String score)
{
    return score;
}

public String player;
public String playerName;
public String cardSuit;
public String cardValue;
public double getScore;

public String getCardSuit() 
{
    return cardSuit;
}

public int getScore() {
    return 0;
}
}

这是我的"测试器",测试我的"玩家类":

public class Tester {

public static void main(String[] args) {

    // Create an object for player 1 
    player p1 = new player("Jack", "h", "k");
    // Create an object for player 2
    player p2 = new player("Jill", "d", "9");
    // Use p1 object and methods getCardSuit and getCardValue to report p1's card
    System.out.println(p1.playerName+"'s card is a "+p1.getCardValue()+" of "+p1.getCardSuit()+"."); 
        // Should print:
            // Jack's card is a King of Hearts.
    // Use p2 object and methods getCardSuit and getCardValue to report p2's card
    System.out.println(p2.playerName+"'s card is a "+p2.getCardValue()+" of "+p2.getCardSuit()+"."); 
        // Should print:    
            // Jill's card is a 9 of Diamonds.

    // Compare player's scores to determine winner
    if(p1.getScore()>p2.getScore())
        System.out.println(p1.playerName+" is the winner!");
    else if (p1.getScore()<p2.getScore());
        System.out.println(p2.playerName+" is the winner!");
        // Should print:
            // "Jack is the winner!"

        }
}

控制台:

null的卡是无效的。Null的卡是无效的。Null是赢家!

  • 我如何修复此"空"内容并实际上打印出其值?非常感谢,事先!:)

我将回答您问的问题:

值是无原始的,因为它们从未初始化。您的player类的构造函数:

public player(String Jack, String h, String k) {
    // does absolutely nothing with the values.
}

什么也不做。这意味着您创建了一个新的播放器player1和一个新的播放器player2,但实际上没有使用这些值来完成任何操作。

player p1 = new player("Jack", "h", "k");

调用构造函数,发送值,值忽略。创建了一个player1的实例,信息绝对什么也没做。因此,您无法将值彼此比较,因为它们尚未初始化。

其他注意事项

当您上课时,在面向对象的编程中,您应该以某种方式思考。

玩家类可以/应该具有String name;变量,然后您可以创建一个播放器对象并传递名称,就像您一样。区别在于您的构造函数:

//Setting up values
String name;
String suit;
String value;
public player(String name, String suit, String value) {
    this.name = name;
    this.suit = suit;
    this.value = value;
}

此技术创建一个带有名称,西装和价值的Player1对象。现在,如果您选择的话,可以将此对象中的值与另一个播放器对象中的相等值进行比较。

,所以我试图稍微清理您的玩家课程:

public class Player 
{
    // The two players are going to enter:
    String p1[] = {"Jack", "h", "k"};
    String p2[] = {"Jill", "d", "9"};
    //Setting up values
    String jack = "Jack";
    String jill = "Jill";
    String h = " Hearts ";
    String d = " Diamonds ";
    String k = " King ";
    int val = 9;
    // Score
    public int score = 0; // State variable score, set equal to 0
    // Player name - Jack, Jill
    public Player(String jack, String h, String k) 
    {
        this.jack = jack;
        this.h = h;
        this.k = k;
    }

    public String playerName(String player)
    {
        // not sure what this is doing?
        // this first line does nothing at all
        // because player gets reassigned to "Jill"
        player = "Jack";
        player = "Jill";
        return player;
    }

    // Card suit
    public String cardSuit(String getcardSuit)
    {
        return cardSuit;
    }
    // Card Value for player 1
    public String getCardValue() 
    {
        return cardValue;
    }
    public String getScore(String score)
    {
        return score;
    }
    public String player;
    public String playerName;
    public String cardSuit;
    public String cardValue;
    public double getScore;
    public String getCardSuit() 
    {
        return cardSuit;
    }
    public int getScore() 
    {
        return 0;
    }
}

您应该开始使用几个约定。班级名称应以大写字母开头。变量和类似的内容都应该具有较低的案例字母(除非是静态的)。

最新更新