Login Register






rock scissors paper filter_list
Author
Message
rock scissors paper #1
i know it's not hard to do and i could have done it in better ways !! but as you all know i'm still learning java i made this game and decided to share it with you just to see your thoughts and opinions and pls tell me if i made anything wrong or if i have some errors!!

Code:
package rockpaperscissors; import java.util.Scanner; public class RockPaperScissors { public static void main(String[] args) { Scanner in = new Scanner(System.in); // player choice System.out.println("Player choice : "); String choicePlayer; choicePlayer = in.nextLine(); while ((!choicePlayer.equalsIgnoreCase("rock")) && (!choicePlayer.equalsIgnoreCase("paper")) && (!choicePlayer.equalsIgnoreCase("scissors"))) { System.out.println("choose rock paper or scissors"); choicePlayer = in.nextLine(); } System.out.println("you're choice is " + choicePlayer); //pc choice double random = Math.random(); String choicePc = null; if (random <= 0.4) { choicePc = "rock"; } if ((random > 0.4) && (random <= 0.7)) { choicePc = "paper"; } if (random > 0.7) { choicePc = "scissors"; } System.out.println("pc choosed " + choicePc); //choosing the winner if (choicePlayer.equals(choicePc)) { System.out.println("its a tie !"); } //if player chose rock if (choicePlayer.equals("rock") && (choicePc.equals("scissors"))) { System.out.println("player won"); } if (choicePlayer.equals("rock") && (choicePc.equals("paper"))) { System.out.println("pc won"); } //if player chose scissors if (choicePlayer.equals("scissors") && (choicePc.equals("paper"))) { System.out.println("player win"); } if (choicePlayer.equals("scissors") && (choicePc.equals("rock"))) { System.out.println("pc won"); } //if player chose paper if (choicePlayer.equals("paper") && (choicePc.equals("rock"))) { System.out.println("player win"); } if (choicePlayer.equals("paper") && (choicePc.equals("scissors"))) { System.out.println("pc won"); } } }
[Image: blackeagle_zps6ad86521.gif]

Reply





Messages In This Thread
rock scissors paper - by blackeagle - 11-03-2013, 03:12 PM