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

Can anyone help with Java and check my code

I want to use a int data type to make all keys other than 1.2.3 invalid for the user in a game of Rock Paper Scissors int Rock = 1; int Paper = 2; int Scissors = 3; turn = 1,2,3;

if(turn == true){ can I use a boolean to do this?

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,076 Points

To answer your question you can use booleans to solve this. However, probably not like how you were thinking you'd use them.

Since you want each of your inputs to be a number representing the move you just have to do an if-if-else-else chain to make sure the user entered the correct input.

// Rock
if(turn == 1) {
   // do something with rock
}
// Paper
else if(turn == 2) { 
   // do something with paper
}
// Scissors
else if(turn == 3) {
   // do something with scissors
}
// Wrong input
else { 
   // The user entered something other than 1 - 3
}

However, you should provide more information about how you are going about creating this rock-paper-scissors game. That way we can figure out if this is the correct approach to solving your problem.

thank you