【问题标题】:One 'cannot find value' stands between me and my first game. [code shown] [closed]我和我的第一场比赛之间有一个“找不到价值”。 [显示代码] [关闭]
【发布时间】:2013-12-19 02:15:31
【问题描述】:

所以我正在构建 Go Fish 的一个版本。我完成了所有的编码,但它不会运行,因为在 Player 类中,我不能在方法中使用 Card.rank 参数。我明白这一点,但我仍然需要能够以某种方式输入排名。

查看我的 Card 构造函数和任何尝试使用 Player 方法的实例,其中我需要输入排名(Player.subSets 和 Player.searchHand)。 (为了简洁,我也只展示了一名玩家的回合代码。)

程序(主)类:

import java.util.*;

public class Program
{
public static void main(String args[]) 
{
    String[] rank = {"two", "three", "four", "five", "six", "seven", "eight",
                 "nine", "ten", "jack", "queen", "king", "ace"};
    String[] suit = {"hearts", "diamonds", "spades", "clubs"};
    Scanner scan = new Scanner(System.in);
    String something = "yes", something2 = "yes", success = "yes"; //Use with while loops
    String turn = "one";
    String temp; //Use with setting names
    String temp2, temp3; //For player being searched
    Card temp4 = new Card(); //For player being searched
    Card[] deck = new Card[52]; //Deck array
    int playercount = 0;
    Player one = new Player("temp");
    Player two = new Player("temp");
    Player three = new Player("temp");
    Player four = new Player("temp");

    //Start game
    while (something.equalsIgnoreCase("yes"))
    {
        //Prepare game
        Card.makeDeck(deck, rank, suit);
        deck = Card.getDeck();
        Card.shuffle(deck);

        while (something2.equalsIgnoreCase("yes") || playercount < 2) //Add players to game
        {
            System.out.println("Would a(nother) player like to join?");
            something2 = scan.nextLine();
            System.out.println();
            if (something2.equalsIgnoreCase("yes"))
            {
                if (playercount <= 4)
                {
                    if (playercount == 0)
                    {
                        System.out.println("What is your name: ");
                        Player one1 = new Player(scan.nextLine());
                        one = one1;
                        playercount++;
                        System.out.println();
                    }
                    else if (playercount == 1)
                    {
                        System.out.println("What is your name: ");
                        Player two2 = new Player(scan.nextLine());
                        two = two2;
                        playercount++;
                        System.out.println();
                    }
                    else if (playercount == 2)
                    {
                        System.out.println("What is your name: ");
                        Player three3 = new Player(scan.nextLine());
                        three = three3;
                        playercount++;
                        System.out.println();
                    }
                    else if (playercount == 3)
                    {
                        System.out.println("What is your name: ");
                        Player four4 = new Player(scan.nextLine());
                        four = four4;
                        playercount++;
                        System.out.println();
                    }
                    else {System.out.println("Only four players are allowed.");
                          something2 = "no";}
                }
            }
            else if (playercount < 2)
            {
                System.out.println("You need at least two players...");
                System.out.println();
            }
        }
        //Deal cards
        if (playercount == 2) 
        {
            for (int i = 1; i < 8; i++)
            {
                one.addCard(Card.draw(deck));
                deck = Card.getDeck();
                two.addCard(Card.draw(deck));
                deck = Card.getDeck();
            }
        }
        else if (playercount == 3) 
        {
            for (int i = 1; i < 8; i++) 
            {
                one.addCard(Card.draw(deck));
                deck = Card.getDeck();
                two.addCard(Card.draw(deck));
                deck = Card.getDeck();
                three.addCard(Card.draw(deck));
                deck = Card.getDeck();
            }
        }
        else
        {
            for (int i = 1; i < 6; i++)
            {
                one.addCard(Card.draw(deck));
                deck = Card.getDeck();
                two.addCard(Card.draw(deck));
                deck = Card.getDeck();
                three.addCard(Card.draw(deck));
                deck = Card.getDeck();
                four.addCard(Card.draw(deck));
                deck = Card.getDeck();
            }
        }
        //Take turns
        while (something.equalsIgnoreCase("yes"));
        {
            success = "yes";
            if (turn.equalsIgnoreCase("one") && something.equalsIgnoreCase("yes"))
                {
                    System.out.println("Player one: " + one.getName() + "'s turn!");
                    System.out.println();
                    //Output hand
                    System.out.println("You have: " + Card.toString(one.getHand()));
                    System.out.println();
                    //Ask what who to search
                    System.out.println("Whose hand would you like to check?");
                    System.out.println("(Enter a player's name.)");
                    temp2 = scan.nextLine(); //Set player name to temp2
                    System.out.println();
                    //Ask what to search
                    while (success.equalsIgnoreCase("yes"))
                    {
                        System.out.println("Remember, you have: " + Card.toString(one.getHand()));
                        System.out.println();
                        System.out.println("Would you like to search for a two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, or ace?");
                        temp3 = scan.nextLine(); //Set desired rank to temp3
                        //Check player two
                        if (temp2.equalsIgnoreCase(two.getName()))
                        {
                            if (two.searchHand(two.getHand(), temp3) != null)
                            {
                                while (two.searchHand(two.getHand(), temp3) != null)
                                {
                                    one.addCard(two.searchHand(two.getHand(), temp3));
                                    two.subCard(two.searchHand(two.getHand(), temp3));
                                    one.subSets(one.getHand(), temp3);
                                }
                            }
                            else if (two.searchHand(two.getHand(), temp3) == null)
                            {
                                System.out.println();
                                System.out.println("Go fish!");
                                System.out.println(one.getName() + " drew a card.");
                                System.out.println();
                                one.addCard(Card.draw(deck));
                                one.subSets(one.getHand(), temp3);
                                deck = Card.getDeck();
                                turn = "two";
                                success = "no";
                                something = one.checkWin();
                                if (something.equalsIgnoreCase("no"))
                                    System.out.println(one.getName() + " won!");
                            }
                        }
                        //Check player three
                        if (temp2.equalsIgnoreCase(three.getName()))
                        {
                            if (three.searchHand(three.getHand(), temp3) != null)
                            {
                                while (three.searchHand(three.getHand(), temp3) != null)
                                {
                                    one.addCard(three.searchHand(three.getHand(), temp3));
                                    three.subCard(three.searchHand(three.getHand(), temp3));
                                    one.subSets(one.getHand(), temp3);
                                }
                            }
                            else if (three.searchHand(three.getHand(), temp3) == null)
                            {
                                System.out.println();
                                System.out.println("Go fish!");
                                System.out.println(one.getName() + " drew a card.");
                                System.out.println();
                                one.addCard(Card.draw(deck));
                                one.subSets(one.getHand(), temp3);
                                deck = Card.getDeck();
                                turn = "two";
                                success = "no";
                                something = one.checkWin();
                                if (something.equalsIgnoreCase("no"))
                                    System.out.println(one.getName() + " won!");
                            }
                        }
                        //Check player four
                        if (temp2.equalsIgnoreCase(four.getName()))
                        {
                            if (four.searchHand(four.getHand(), temp3) != null)
                            {
                                while (four.searchHand(four.getHand(), temp3) != null)
                                {
                                    one.addCard(four.searchHand(four.getHand(), temp3));
                                    four.subCard(four.searchHand(four.getHand(), temp3));
                                    one.subSets(one.getHand(), temp3);
                                }
                            }
                            else if (four.searchHand(four.getHand(), temp3) == null)
                            {
                                System.out.println();
                                System.out.println("Go fish!");
                                System.out.println(one.getName() + " drew a card.");
                                System.out.println();
                                one.addCard(Card.draw(deck));
                                one.subSets(one.getHand(), temp3);
                                deck = Card.getDeck();
                                turn = "two";
                                success = "no";
                                something = one.checkWin();
                                if (something.equalsIgnoreCase("no"))
                                    System.out.println(one.getName() + " won!");
                            }
                        }
                    }
                }
           //ALL THE CODE FOR OTHER TURNS HERE
        }
        //Replay
        System.out.println("Would you like to play again?");
        something = scan.nextLine();
    }
}

}

播放器类:

import java.util.*;

public class Player
{
private static String name;
private static Card[] hand = new Card[52];
private static Card[] sets = new Card[52];
private static int handsize = 0, nullcount = 0;
private static String win = "no"; 
private static int temp = 1; //For subCard
private static int temp2 = 0, temp3 = 0; //For subSets

//Constructor
public Player(String n)
{
    name = n;
}

//Mutators
//Add new card to hand
public static void addCard(Card c)
{
    hand[handsize] = c;
    handsize++;
}
//Lose card that was searched for
public static void subCard(Card c)
{
    for (int i = 0; i < hand.length; i++)
    {
        if (c == hand[i])
        {
            temp = i;
            if (temp < 51)
                temp++;
             hand[i] = hand[temp];
        }
    }
    hand[(hand.length - 1)] = null;
    handsize--;
}
//Lose sets of cards
public static void subSets(Card[] h, Card.rank r)
{
    //See if there are four of a kind
    for (int i = 0; i < h.length; i++)
    {
        if (h[i].rank == r)
        {
            temp2++;
        }
    }
    //Drop set of four
    if (temp2 == 4)
    {
        for (int i = 0; i < h.length; i++)
        {
            if (h[i].rank == r)
                h[i] = null;
            for (int ii = (i + 1); ii < (h.length - 1); ii++)
            {
                temp3 = i;
                h[temp3] = h[ii];
                temp3++;
            }
            h[(h.length - 1)] = null;
        }
    }
    hand = h;
}

//Accessors
public static String getName()
{
    return name;   
}
public Card[] getHand()
{
    return hand;   
}
public Card searchHand(Card[] h, Card.rank r)
{
    for (int i = 0; i < h.length; i++)
    {
        if (h[i].rank == r)
            return h[i];
        else if (i == h.length - 1)
            return null;
    }
}
public String checkWin()
{
    for (int i = 0; i < hand.length; i++)
    {
        if (hand[i] == null)
            nullcount++;
    }
    if (nullcount == (hand.length - 1))
        return "no"; //Set equal to something in Program
    else
    {
        nullcount = 0;
        return "yes"; //Set equal to something in Program
    }
}
}

卡类:

import java.util.*;

public class Card
{   
public String suit;
public String rank;
private static int temp = 0, temp2 = 0; //Use for reseting rank and suit
private static Card temp3; //Use for draw method
private static int temp4 = 1; //Use for draw and shuffle, always back to 1
private static Card[] deck = new Card[52];
private static Random random = new Random();

//Constructors
public Card()
{
    this.rank = "two";
    this.suit = "hearts";
}
public Card(String r, String s)
{
    this.rank = r;
    this.suit = s;
}

//Mutators
//Make deck
public static void makeDeck(Card[] c, String[] r, String[] s)
{
    for (int i = 0; i < c.length; i++)
    {
        c[i] = new Card(r[temp], s[temp2]);
        temp++; temp2++;
        //Reset rank and suit
        if (temp > 12)
            temp = 0;
        if (temp2 > 3)
            temp2 = 0;
    }
    deck = c;
}

//Accessors
//Return deck
public static Card[] getDeck()
{
    return deck;   
}
//Shuffle
public static Card[] shuffle(Card[] c) 
{
    for (int i = 0; i < c.length; i++)
    {
        int rand = (int)(random.nextInt(52)*(i + 1));
        //Don't let anything be in a slot that doesn't exist
        while (rand > c.length)
        {
            temp4 = random.nextInt(52);
            rand -= temp4;
        }
        if (rand < 0)
        {
            rand += temp4;
            Card temp = c[i];
            c[i] = c[rand];
            c[rand] = temp;
        }
    }
    deck = c;
    temp4 = 1;
    return deck;  
}
//Draw
public static Card draw(Card[] c)
{
    temp3 = c[0];
    for (int i = 0; i < c.length - 1; i++)
    {
        c[i] = c[temp4];
        if (temp4 < 51)
            temp4++;
    }
    c[(c.length - 1)] = null;
    temp4 = 1;
    deck = c;
    return temp3;
}
//Return string
public static String toString(Card[] h) 
{
    String temp5 = "yes";
    while (temp5.equals("yes"))
    {
        for (int i; i < h.length; i++)
        {
            if (h[i] != null)
                return h[i].rank + " of " + h[i].suit +", ";
            else temp5 = "no";
        }
    }
}
}

【问题讨论】:

  • 我敢问 - 有什么问题?
  • Player.java 43 null 错误:找不到符号 symbol null null class rank location null null class Card Player.java 81 null error:找不到 symbol symbol null null class rank location null null class Card @user2310289 .它仅适用于 Player.subSets 和 Player.searchHand
  • @ElliottFrisch,这是我昨晚遇到的另一个问题。现在我的游戏已经完成了,但是 Player.subSets 和 Player.searchHand 不能使用排名参数
  • 最近几天这段代码被发布了多少次?

标签: java


【解决方案1】:

Card.rank 是一个字符串

public class Card
{   
public String rank;

所以方法声明需要

public static void subSets(Card[] h, String r)

至于

 public Card searchHand(Card[] h, Card.rank r)
 {
    for (int i = 0; i < h.length; i++)
    {
        if (h[i].rank == r)
            return h[i];
        else if (i == h.length - 1)
            return null;
    }
 }

如果 h.length 为零,你要返回什么?

【讨论】:

  • 谢谢。修复了它,但现在唯一的错误是 Player.searchHand 的“缺少返回语句”。我认为这是因为我拥有的 return 语句受到保护。我想我可以自己解决这个问题。编辑:h 在我的游戏中永远不会为零
  • 为你更新了我的答案。
  • 谢谢。我会调查的,@user2310289
猜你喜欢
  • 2022-11-23
  • 2012-03-27
  • 2022-01-08
  • 2018-06-29
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 2022-06-25
  • 1970-01-01
相关资源
最近更新 更多