Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Java

I have a project in which we compare three cards and compare color(String) and we have to make sure that the player with color blue wins. How do I compare those strings to make blue the superior?

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Vrund;

After thinking about this today I think 'enums' may be a better solution for your situation. There is a decent tutorial on them here.

Ken

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Vrund;

Are the card colors constant? So you always use red, yellow, and blue for example?

If you haven't taken the latest Java course on Data Structures I would highly suggest it. There are a few solutions available in there that I can think of that would ultimately make your code better than running through a bunch of if statements.

Ken

There's an array of cards with three different colors and when a method is called a card is assigned to each player and then compare the color.

Ken Alger
Ken Alger
Treehouse Teacher

The new Data Structures course covers the use of arrays but also sets and maps. You could set up a map with key/value pairs that would be similar to:

"blue" => 10

"yellow" => 0

"red" => 0

And then evaluate them that way. Just a thought.

Ken

Thanks Ken. I will take a look at that.