Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-21-2008, 12:49 AM   PM User | #1
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
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
webguy08 is offline   Reply With Quote
Old 11-21-2008, 02:08 AM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
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.
brad211987 is offline   Reply With Quote
Users who have thanked brad211987 for this post:
webguy08 (11-21-2008)
Old 11-21-2008, 08:00 PM   PM User | #3
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
Thanks for that!
webguy08 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:51 PM.


Advertisement
Log in to turn off these ads.