Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post

    Array: Can't use > or < ?

    I'm writing a piece of code for a game which I am a creating: just a simple game where the player must guess higher or lower for the next card in the sequence.

    I am just 1 step away from it working, however I am very confused as to how I can tackle the problem I am faced with.

    Here is the code:

    Code:
    /**
     * This class models a player.
     */
    public class Player
    {
        private String name;
        private Card[] card = new Card[5];
        private int score = 0;
        
        /**
         * Contructor for objects of class Player.
         * @param A String which initialises the name of the player.
         */
        public Player(String name)
        {
            this.name = name;
        }
        
        /**
         * Gets the score of the player.
         * @return An integer value representing the score of the player.
         */
        public int getScore()
        {
            return score;
        }
        
        /**
         * Gets the name of the player.
         * @return A String representing the name of the player.
         */
        public String getName()
        {
            return name;
        }
        
        /**
         * Gives the player 5 cards from the pack.
         */
        private void fill()
        {
            PackOfCards deck = new PackOfCards();
            int i = 0;
            while(i < card.length) {
                card[i] = deck.selectCard();
                i++;
            }
        }
        
        /**
         * Calls the fill() method and creates the method to enable the player to play the game.
         */
        public void play()
        {
            fill();
            Reader reader = new Reader();
            boolean stillPlaying = true;
            System.out.println(card[0].toString());
            int index = 1;
            while(stillPlaying = true) {
                if(reader.equals("higher") /*&& is correct*/) {
                    if(card[index] > card[index - 1]) {
                        score++;
                        index++;
                        System.out.println(card[index].toString());
                    }
                }
                if(reader.equals("higher") /*&& is wrong*/) {
                        score = 0;
                        index++;
                        System.out.println(card[index].toString());
                        stillPlaying = false;
                }
                if(reader.equals("lower") /*&& is correct*/) {
                    score++;
                    index++;
                    System.out.println(card[index].toString());
                }
                if(reader.equals("lower") /*&& is wrong*/) {
                    score = 0;
                    index++;
                    System.out.println(card[index].toString());
                    stillPlaying = false;
                }
                if(reader.equals("freeze")) {
                    stillPlaying = false;
                }
            }
            Reader.close();
        }
    }
    I coloured the text that is causing the error in red. I want it to do:
    if(the current card is greater than the previous card) {
    }
    However, I am unsure how to do so. I get an error which complains about the > operator.

    Would anyone mind helping me?

    Cheers

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    To answer correctly, we will need to see your Card class. To take a guess though, you will need to do something similar to:

    Code:
    if (card[index].getValue() > card[index-1].getValue())
    This is assuming your Card class has a value stored as an instance variable, and you have declared a getValue method that will return that value for your use in a calling program.

  • Users who have thanked brad211987 for this post:

    webguy08 (11-21-2008)

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Thanks for that!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •