|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Here is my code:
import java.util.Scanner; import java.util.Random; public class RockPaperScissors { public static final int ROCK = 1; public static final int PAPER = 2; public static final int SCISSORS = 3; // // Plays Rock-Paper-Scissors game with the user // public static void main(String[] args) { final int NUM_CHOICES = 3; Random generator = new Random(); int userWins = 0; int compWins = 0; int ties = 0; int compOption, userOption; boolean exit = false; Random randGen = new Random(); Scanner scan = new Scanner(System.in); System.out.println("The Rock-Paper-Scissors Game"); System.out.println("Enter your choice: "); userOption = scan.nextInt(); System.out.println("Computers Choice: "); compOption = scan.nextInt(); //Prompt game results System.out.println("-------------------------------------------"); System.out.println("Game Results"); System.out.println("User won: " + userWins + " times"); System.out.println("Computer won: " + compWins + " times"); System.out.println("User and Computer Tied: " + ties + " times"); System.out.println("-------------------------------------------"); } // method main // // Method displays options to user. // public static void promptOptions() { System.out.println("-------------------------------------------"); System.out.println("1. Rock"); System.out.println("2. Paper"); System.out.println("3. Scissors"); System.out.println("4. Exit"); System.out.print("Enter Option: "); } // method promptOptions } i am having trouble with how to set up the rock, paper and scissor so that it can be played. i am also not sure how to set up my "if" statement. it has me a little confused so if i could get some hints that would be awesome! |
|
#2
|
||||
|
||||
|
I don't know the exact rules of the game, so I'll leave
that part to you. But what I would do is write a second class, for example RockGame. This class would accept the choice of the user, the choice of the computer and keep the score. So, you'll need some properties, getters and setters. You could even let the class RockGame produce a random choice of the computer. You should add a while loop to play the game. As long as the user doesn't enter a certain key, he/she will keep on playing. Inside the while loop, you can use the second class RockGame or whatever you want to call it. The user enters his/her choice, and RockGame produces the random choice of the computer, decides who won and notes this in the score. As soon as the user enters the key to stop the game, RockGame outputs the final score. The advantage of using a second class is that you're getting a feeling of how to work with classes and objects. The way you are writing it now, is more the procedural way. I don't mean it's bad but Java is conceived to let you build classes and reuse them. ANd with RockGame you could give this way of programming a try. |
|
#3
|
||||
|
||||
|
What happens when the code you have is run?
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > I need help with my rock paper scissors program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|