Human machine Scissors (player, computer, game, test) four types of writing

Source: Internet
Author: User

First class, Player class:

ImportJava.util.Scanner;/*** Create player class*/ Public classPerson {Scanner input=NewScanner (system.in); /*** Define the properties of the player class*/String name; intscore;    String Action; intnum; /*** Define the scissors method of the player class*/     Public voidmethod () {System.out.println ("\ n Please punch: 1. Scissors 2. Stone 3. Cloth"); BooleanA =true;  Do{num=Input.nextint (); if(num = = 1 | | num = 2 | | num = = 3) {                Switch(num) { Case1: Action= "Scissors";  Break;  Case2: Action= "Stone";  Break;  Case3: Action= "Cloth";  Break; } A=false; System.out.println ("You punch out:" +action); } Else{System.out.println ("Input number is wrong, please re-enter"); }        }  while(a); }}

Second class, Computer class:

    /*** Computer class*/ Public classComputer {/*** Define the properties of the computer class*/String name; intscore;    String Action; intnum; /*** Define the Scissors method of computer class*/     Public voidmethod () {/*** Random generation of digital 123*/Num=(int) ((Math.random ()) +1; Switch(num) { Case1: Action= "Scissors";  Break;  Case2: Action= "Stone";  Break;  Case3: Action= "Cloth";  Break; } System.out.println (Name+ "Punch out:" +action); }}                                    

Third class, Game class:

ImportJava.util.Scanner;/*** Game Class*/ Public classGame {Scanner input=NewScanner (system.in); /*** Define the properties of the game class*/ Person Person=NewPerson ();//creating objects for the Player classComputer computer =NewComputer ();//creating objects for a computer class    intNumber ; intFrequency = 0; /*** How to define the game process*/     Public voidprocess () {System.out. println ("--------------------------welcome into the game world--------------------------\ n"); System.out.println ("\t\t********************************"); System.out.println ("\t\t********** scissors, start *************"); System.out.println ("\t\t********************************");        System.out.println (); System.out.println ("Punching rule: 1. Scissors 2. Stone 3. Cloth"); System.out.print ("Please select the opponent's role (1: Liu Bei 2: Sun Quan 3: Caocao):"); Booleanb =true;  Do{ number=Input.nextint (); if(number = = 1 | | number = = 2 | | number = = 3) {                Switch(number) { Case1: Computer.name= "Liu Bei";  Break;  Case2: Computer.name= "Sun Quan";  Break;  Case3: Computer.name= "Caocao";  Break; } b=false; } Else{System.out.println ("Input number is wrong, please re-enter"); }        }  while(b); System.out.print ("Please enter your name:"); Person.name=Input.next (); System.out.println (Person.name+ "VS" + computer.name + "PVP \ n"); System.out.println ("Do you want to start?" (y/n) "); CharAnswer = Input.next (). CharAt (0);  while(Answer = = ' Y ') {Person.method ();            Computer.method (); if(Person.num = =computer.num) {System.out.println ("Hey, draw, wait and see!" "); } Else if((Person.num = = 2) && (Computer.num = = 1))                    || (Person.num = = 1) && (Computer.num = = 3)                    || ((Person.num = = 3) && (Computer.num = = 2)) {System.out.println ("Wow, you won, that's great!" "); Person.score++; } Else{System.out.println ("^_^!!! You're a loser, stupid! "); Computer.score++; } Frequency++; System.out.println ("\ n"); System.out.println ("Do you want to continue?" (y/n) "); Answer= Input.next (). CharAt (0); }    }    /*** How to define game billing*/     Public voidShowresult () {System.out.println ("********************************"); System.out.println (Person.name+ "VS" +computer.name); System.out.println ("PvP Times:" +frequency); System.out.println ("\ n name \t\t score"); System.out.println (Person.name+ "\t\t" +Person.score); System.out.println (Computer.name+ "\t\t" +Computer.score); if(Person.score <computer.score) {System.out.println ("Oh, stupid, next refueling!" "); } Else if(Person.score = =computer.score) {System.out.println ("Wow, even a draw, our next showdown!" "); } Else{System.out.println ("Wow, you're great!" "); } System.out.println ("********************************"); }}

Test class:

 Public class Demo {publicstaticvoid  main (string[] args) {    Game play=New Game (); // creating Objects    for game classes Play.process (); // Call Game Process method    of Game class Play.showresult (); // call the game class's billing method }}

Human machine Scissors (player, computer, game, test) four types of writing

Related Article

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.