Scissors game three innings and two wins------Java implementation code

Source: Internet
Author: User

Package Com.javasm.exerices02;import Java.util.arraylist;import Java.util.list;import java.util.Random;import java.util.scanner;/** * *todo stone scissors cloth scissors game, three innings two wins * @author Caolei May 2, 2018 afternoon 10:51:23 * RockPaperScissors */public class Rock Paperscissors {//For storing scissors result private static list<string> gameresult;public static void Crguessinggame (Scanner SC) { Gameresult = new arraylist<string> ();//store result set char[] finger = {' Hammer ', ' cut ', ' package '};//used to receive each scissors result String result;//for computer Scissors random random = new random ();//record computer wins int computerwinnumber = 0;//count player wins int playerwinnumber = 0;//record scissors First round int i = 0;//Loop out fist while (true) {System.out.println ("+ (i + 1) +" turn, please punch: ");//Computer generated random number int computerget = Random.nextint (3);//user output into the number int playerinput = Sc.nextint ();//Record scissors result = Finger[computerget] + "," + finger[playerinput];//put the result into the collection Gameresult . Add (Result),/* * First to determine results by if * * if (Computerget = = 0 && playerinput! = 0) {* if (playerinput = = 1) * Computer  winnumber++; * playerwinnumber++; *  * } else if (Computerget = = 1 && playerinput! = 1) {* if (playerinput = = 2) * computerwinnumber++;  * playerwinnumber++;  *} else if (Computerget = = 2 && playerinput! = 2) {* if (Playerinput = = 0) * computerwinnumber++;  * playerwinnumber++; *} *//** * The second result */switch (computerget) {case 0:if (playerinput = 1) computerwinnumber++;if (playerinput = 2) p Layerwinnumber++;break;case 1:if (Playerinput = = 2) computerwinnumber++;if (playerinput = 0) playerWinNumber++;break; Case 2:if (Playerinput = = 0) computerwinnumber++;if (playerinput = = 1) playerwinnumber++;break;default:break;} Record the number of game rounds i++;//determine if anyone wins if (Computerwinnumber = = 2 | | playerwinnumber = = 2) {break;}} Judging the winner if (Computerwinnumber > Playerwinnumber) {System.out.println ("Computer wins!") ");} else {System.out.println ("Congratulations on your victory! ");} Output scissors Results System.out.println ("Concrete results are as follows:"); SYSTEM.OUT.PRINTLN ("Computer, you"); for (String Results:gameresult) {System.out.println ("{" + results + "}");}}}

  

Scissors game three innings and two wins------Java implementation code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.