The first object-oriented program:
One console scissors mini-game:
The first step is to select the role;
The second part chooses the scissors, the stone, the cloth, and the computer carries on the PK;
The third choice to continue or choose to end;
The end shows the number of innings, and the points earned by each game;
Design ideas
Analyze the problem and design four classes
1. Design of person,computer,game three classes;
2. Design the test class start program;
Test run:
-----------Welcome to the game world-----------****************** ****** scissors Start * * * * * * ******************----------------- -----------------------Punch Rules:1. Scissors 2. Stone 3. Cloth Please select the role: (1. Liu Bei 2. Sun Quan 3. Caocao):3you choose the Devil to battle input y start: y , please give me a punch:1. Scissors 2. Stone 3. Cloth (Please enter the corresponding number:)1you punch: Scissors computer punch: scissors and draw, another game whether to start the next round (Y/N):y please punch:1. Scissors 2. Stone 3. Cloth (Please enter the corresponding number:)2you punch: the stone computer punch: The cloth you lose, really stupid!whether to start the next round (Y/N):y please punch:1. Scissors 2. Stone 3. Cloth (Please enter the corresponding number:)3You punch: Cloth computer punch: cloth and draw, and then a game whether to start the next round (Y/N):n you have chosen: Caocao you total War number:3you have a total of: 0 points Computer Total: 1 points you won 0 innings, you lost 1 innings and 2 innings, your 1th inning: Scissors computer out: Scissors your 2nd inning: The stone computer out of: cloth you 3rd out of: Cloth computer out of: cloth
Person class:
Packagecom.tt;ImportJava.util.*; Public classPerson {Scanner input=NewScanner (system.in); Public Static intScore = 0; Public intShowselect () { while(true) {System.out.print ("Please punch: 1. Scissors 2. Stone 3. Cloth (Please enter the corresponding number:) \ n"); intnum =Input.nextint (); Switch(num) { Case1: System.out.println ("You punch: Scissors."); return1; Case2: System.out.println ("You Punch: Stone"); return2; Case3: System.out.println ("You punch: Cloth"); return3; default: System.out.println ("Wrong input, please re-enter \ n"); Continue;//input error, re-enter } } }}
Computer class:
Packagecom.tt;ImportJava.util.*; Public classComputer {Scanner input=NewScanner (system.in); Public Static intscore; Public intShowselect () {intnum = (int) (Math.random () * 3 + 1); Switch(num) { Case1: System.out.println ("Computer Punch: Scissors"); return1; Case2: System.out.println ("Computer Punch: Stone"); return2; Case3: System.out.println ("Computer Punch: Cloth"); return3; default: return-1; } }}
Game class:
Packagecom.tt;ImportJava.util.*; Public classGame {Scanner input=NewScanner (system.in); PrivatePerson person =NewPerson (); PrivateComputer computer =Newcomputer (); Private Static intCount = 0;//record the number of matches Private Static intWin_count = 0; Private Static intLose_count = 0; Private Static intPeace_count = 0; ArrayList<Integer> List1 =NewArraylist<integer> ();//record the number of people each time outArraylist<integer> List2 =NewArraylist<integer> ();//record the number of each computer out PublicString Show () {System.out.println ("-----------welcome into the game world-----------"); System.out. println ("\t\t******************\n\t\t****** Scissors begins *****\n\t\t******************"); System.out.println ("----------------------------------------\ n"); System.out.println ("Punching rule: 1. Scissors 2. Stone 3. Cloth"); while(true) {System.out.println ("Please select the role: (1. Liu Bei 2. Sun Quan 3. Cao):"); intnum =Input.nextint (); Switch(num) { Case1: System.out.println ("You choose Liu Bei to battle"); return"Liu Bei"; Case2: System.out.println ("You chose Sun Quan to battle"); return"Sun Quan"; Case3: System.out.println ("You choose to fight the devil"); return"Caocao"; default: System.out.println ("You entered the wrong, please re-enter Kazakhstan"); Continue;//input error, re-enter } } } Public voidStart (String s) {System.out.println ("Enter Y to start:"); while(true) {String answer=Input.next (); if(Answer.equals ("Y")) { intPerson_select =Person.showselect (); intComputer_select =Computer.showselect (); if(Person_select = = 1 && computer_select = = 1) || (Person_select = = 2 && computer_select = 2) || (Person_select = = 3 && computer_select = 3) {System.out.println ("Draw, let's get another game."); Peace_count++; } Else if(Person_select = = 1 && computer_select = = 3) || (Person_select = = 2 && computer_select = 1) || (Person_select = = 3 && computer_select = 2) {System.out.println ("Congratulations, you won!" Come on, \ n); Person.score++; Win_count++; } Else{System.out.println ("You lost, stupid!\n."); Computer.score++; Lose_count++; } list1.add (Person_select);//add to the record person's arrayList2.add (Computer_select);//add to the computer's array of peoplecount++; System.out.print ("Whether to start the next round (y/n):"); } Else{System.out.println ("\ n You chose:" + S + "You total War number:" +count); System.out.println ("You get Together:" + person.score + "min"); System.out.println ("Computer together:" + Computer.score + "points"); System.out.println ("You won" + Win_count + "bureau"); System.out.println ("You lost" + Lose_count + "bureau"); System.out.println ("Draw" + Peace_count + "bureau"); //show people and computers each time the battle for(inti = 0; I < list1.size (); i++) {System.out.println ("You first" + (i + 1) + "bureau out of:" + Great (list1.get (i)) + "Computer out of:" +Great (List2.get (i))); } } } } //used to show the race process PublicString Great (intnum) { Switch(num) { Case1: returnScissors; Case2: returnStone; Case3: returnCloth; default: return NULL; } }}
Test class:
Package com.tt; Public class Test { publicstaticvoid main (string[] args) { new Game (); = G1. Show (); G1. Start (s);} }
Java object-oriented thinking to solve scissors problem