0

这是玩我制作的猜谜游戏的代码,但问题是我作为java初学者不擅长的几个问题需要一些指导。沿着代码有一些错误,我用侧面的箭头突出显示。

    import java.util.*;

public class GuessingGame
{


    private static Player house;
    private static Player player;

    private static int wins;
    private static int loses;
    private String name;
    int card1,card2;
    private int value;



    public void Player(String name){

        this.name=name;
        card1 = (Integer) null;
        card2 = (Integer) null;
    }



public void Card(int value){

     this.value = value;
    }





public int getValue(){
            return value;
        }



public void acceptDeal(Card card1, Card card2){
        Random r = new Random();
        int value = r.nextInt(13) + 1;
        card1 = new Card(value);            <<<<<<<<======= Error 1
        value = r.nextInt(13) + 1;
        card2 = new Card(value);            <<<<<<<<======= Error 2
    }



public static void init()
{

    house = new Player("House");                 <<<<<<<<======= Error 3
    player = new Player("Player");               <<<<<<<<======= Error 4
    wins = 0;
    loses = 0;

}


    public static void playGame() 
    {
       Scanner scan = new Scanner(System.in);

        char option, playAgain;
        int houseHandStrength, playerHandStrength;
        System.out.println("Welcome to our card guess 1.0 game!");
        System.out.println();

        do {
            // Deal cards to the house and player.
            house.acceptDeal(new Card(houseHandStrength), new Card(houseHandStrength));    <<<<<=== Error 5
            player.acceptDeal(new Card(playerHandStrength), new Card(playerHandStrength));  <<<<<=== Error 6    

            System.out.println(house);

            // Determine whether the player wants to play this hand.
            do {
                System.out.print("Deal cards? (Y/N) ");
                option = Character.toLowerCase(scan.next().charAt(0));
            }
            while (option != 'n' && option != 'y');

            if (option == 'y')
            {
                System.out.println(player);

                // Display hand strength of both players.
                houseHandStrength = house.getHandStrength();    <<<<<=== Error 7
                playerHandStrength = player.getHandStrength();  <<<<<=== Error 8
                System.out.println("The dealer's hand strength is: " + houseHandStrength);
                System.out.println("Your hand strength is: " + playerHandStrength);
                System.out.println();

                // If the player has a stronger hand.
                if (player.getHandStrength() > house.getHandStrength())
                {
                    System.out.println("** You won the hand! **");
                    wins++;
                }
                else {
                    System.out.println("The house wins this round!");
                    loses++;
                }
            }

            // Display the win/lose statistics.
            System.out.println("Current wins: " + wins);
            System.out.println("Current loses: " + loses);

            // Prompt whether the user wants to play again.
            do {
                System.out.print("Would you like to play again? (Y/N) ");
                playAgain = Character.toLowerCase(scan.next().charAt(0));
            }
            while (playAgain != 'n' && playAgain != 'y');           

            System.out.println();
            System.out.println("*******************************************************");
        }
        while (playAgain == 'y');

        System.out.println();
        System.out.println("Thank you for playing!");
    }

    public static void main(String[] args)
    {
        init();
        playGame();
    }
}
4

4 回答 4

1

首先欢迎来到 StackOverflow。很高兴看到您找到并使用了 homework 标签。请记住,为了让人们能够帮助您,您需要提供更多信息。错误是什么意思,运行代码时会发生什么等

关于你得到的错误,看起来你还没有真正定义类CardPlayer你的代码中有两个方法GuessingGame.Card()GuessingGame.Player()你的GuessingGame类。将它们更改为内部(或外部)类,应该没问题;)

于 2012-04-26T11:55:31.017 回答
1

也许您需要在顶部导入其他类?

问题似乎只存在于您自己的课程中,程序输出对错误有什么看法?

public void Player(String name)……和 public void Card(int value)……

应该上课吧?将它们声明为另一个文件中的类并将它们包含到主文件中。

于 2012-04-26T11:55:48.300 回答
1

在您之前的问题 card1card2,类型为Card. 没错,现在你已经改变了这一点,现在它是错误的。

于 2012-04-26T11:56:22.123 回答
1

你似乎已经把你的代码打包了。您已经组合了 Player、Card 和 Game 类。我手边没有 Java 编译器,但您要做的是分解这三个模型。

错误 1-6 是在类甚至不存在时尝试实例化新对象的结果。错误 7-8 是由于尝试调用同一方法的结果。

import java.util.*;

class Player {
    int card1, card2;
    private String name;

    public void Player(String name){
        this.name=name;
        card1 = (Integer) null;
        card2 = (Integer) null;
    }

    public void acceptDeal(Card card1, Card card2){
        Random r = new Random();
        int value = r.nextInt(13) + 1;
        card1 = new Card(value);            <<<<<<<<======= Error 1
        value = r.nextInt(13) + 1;
        card2 = new Card(value);            <<<<<<<<======= Error 2
    }
}


class Card {
    private int value;

    public void Card(int value){
        this.value = value;
    }

    public int getValue(){
        return value;
    }
}


public class GuessingGame
{
    private static Player house;
    private static Player player;
    private static int wins;
    private static int loses;

    public static void init()
    {
        house = new Player("House");                 <<<<<<<<======= Error 3
        player = new Player("Player");               <<<<<<<<======= Error 4
        wins = 0;
        loses = 0;
    }

    public static void playGame() 
    {
        Scanner scan = new Scanner(System.in);

        char option, playAgain;
        int houseHandStrength, playerHandStrength;
        System.out.println("Welcome to our card guess 1.0 game!");
        System.out.println();

        do {
            // Deal cards to the house and player.
            house.acceptDeal(new Card(houseHandStrength), new Card(houseHandStrength));    <<<<<=== Error 5
            player.acceptDeal(new Card(playerHandStrength), new Card(playerHandStrength));  <<<<<=== Error 6    

            System.out.println(house);

            // Determine whether the player wants to play this hand.
            do {
                System.out.print("Deal cards? (Y/N) ");
                option = Character.toLowerCase(scan.next().charAt(0));
            }
            while (option != 'n' && option != 'y');

            if (option == 'y')
            {
                System.out.println(player);

                // Display hand strength of both players.
                houseHandStrength = house.getHandStrength();    <<<<<=== Error 7
                playerHandStrength = player.getHandStrength();  <<<<<=== Error 8
                System.out.println("The dealer's hand strength is: " + houseHandStrength);
                System.out.println("Your hand strength is: " + playerHandStrength);
                System.out.println();

                // If the player has a stronger hand.
                if (player.getHandStrength() > house.getHandStrength())
                {
                    System.out.println("** You won the hand! **");
                    wins++;
                }
                else {
                    System.out.println("The house wins this round!");
                    loses++;
                }
            }

            // Display the win/lose statistics.
            System.out.println("Current wins: " + wins);
            System.out.println("Current loses: " + loses);

            // Prompt whether the user wants to play again.
            do {
                System.out.print("Would you like to play again? (Y/N) ");
                playAgain = Character.toLowerCase(scan.next().charAt(0));
            }
            while (playAgain != 'n' && playAgain != 'y');           

            System.out.println();
            System.out.println("*******************************************************");
        }
        while (playAgain == 'y');

        System.out.println();
        System.out.println("Thank you for playing!");
    }

    public static void main(String[] args)
    {
        init();
        playGame();
    }
}
于 2012-04-26T12:23:23.210 回答