猜猜游戏java类的问题〜

时间:2012-04-26 11:50:40

标签: java

这是一个我玩的猜谜游戏的代码,但问题是我作为初学者在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 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

问题似乎只出现在你自己的课程中,程序输出对错误的评价是什么?

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

应该是正确的课程吗?将它们声明为另一个文件中的类,并将它们包含在主文件中。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

你好像已经把你的代码捆起来了。你已经结合了玩家,卡片和游戏课程。我没有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();
    }
}