Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. //start game
  2.         System.out.println("Let's play Rock, Paper, Scissors!");
  3.         System.out.println("Say \"Rock\", \"Paper\", or \"Scissors\" to indicate your choice. Otherwise say \"Quit\" to quit.");
  4.         Scanner input = new Scanner(System.in);
  5.         String choice = input.nextLine(); //prompt response
  6.         choice = choice.toLowerCase(); //change to lowercase for consistency
  7.        
  8.         //initialize variables
  9.         int tienum = 0;
  10.         int winnum = 0;
  11.         int lossnum = 0;
  12.  
  13.         while (!choice.equals("quit")) //do the following if the user does not put in "quit"
  14.         {
  15.             int choicenum = 0;
  16.             if (choice.equals("rock")) //assign numbers to string
  17.             {
  18.                 choicenum = 1;
  19.             }
  20.             else if (choice.equals("paper")) //assign numbers to string
  21.             {
  22.                 choicenum = 2;
  23.             }
  24.             else if (choice.equals("scissors"))//assign numbers to string
  25.             {
  26.                 choicenum = 3;
  27.             }
  28.             else //not valid responses
  29.             {
  30.                 while(choicenum == 0) //continue while user input is still not valid
  31.                 {
  32.                     System.out.println("Sorry, it looks like you didn't enter a correct input. Try again.");
  33.                     choice = input.nextLine();
  34.                     choice = choice.toLowerCase();
  35.                     if (choice.equals("rock"))
  36.                     {
  37.                         choicenum = 1;
  38.                     }
  39.                     else if (choice.equals("paper"))
  40.                     {
  41.                         choicenum = 2;
  42.                     }
  43.                     else if (choice.equals("scissors"))
  44.                     {
  45.                         choicenum = 3;
  46.                     }
  47.                     else if (choice.equals("quit"))
  48.                     System.exit(0); //quit program
  49.                 }
  50.             }
  51.             int compnum = (int) (Math.random()*3) + 1;//computer generate random num
  52.             //print computer choice
  53.             if (compnum == 1) System.out.println("Computer chose rock");
  54.             if (compnum == 2) System.out.println("Computer chose paper");
  55.             if (compnum == 3) System.out.println("Computer chose scissors");
  56.        
  57.            
  58.             if(choicenum == compnum) //tie cases
  59.                 {
  60.                     System.out.println("It's a tie");
  61.                     tienum++;
  62.                 }
  63.             else if (choicenum == 1 && compnum == 3) //user wins rock vs scissors
  64.                 {
  65.                     System.out.println("you win!");
  66.                     winnum++;
  67.                 }
  68.             else if (choicenum == 3 && compnum == 2) //user wins scissors vs paper
  69.                 {
  70.                     System.out.println("you win!");
  71.                     winnum++;
  72.                 }
  73.             else if (choicenum == 2 && compnum ==1) //user wins paper vs rock
  74.                 {
  75.                     System.out.println("you win!");
  76.                     winnum++;
  77.                 }
  78.             else //otherwise computer wins
  79.                 {
  80.                     System.out.println("you lose.");
  81.                     lossnum++;
  82.                 }
  83.             System.out.println("wins:" + winnum + "\nloses:" + lossnum + "\nties:" + tienum); //print out number of wins, ties, and loses
  84.             System.out.println("Let's play again! \n \n"); //start game again
  85.             System.out.println("Say \"Rock\", \"Paper\", or \"Scissors\" to indicate your choice. Otherwise say \"Quit\" to quit.");
  86.             choice = input.nextLine(); //prompt for new user input
  87.             choice = choice.toLowerCase();
  88.         }
  89.         if(choice.equals("quit")) //if user prints "quit", then quit program
  90.         System.exit(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement