2016-07-02
Packagecom.java1995; Public classPlayer {//Package PrivateString name;//player Name Private intScore;//points//Constructors PublicPlayer (String name,intscore) { Super(); This. Name =name; This. score =score; } //Implementing Encapsulation PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetscore () {returnscore; } Public voidSetScore (intscore) { This. score =score; } //ToString Print Rule@Override PublicString toString () {//TODO auto-generated Method Stub return This. GetName () + ":" + This. Getscore (); }}
Packagecom.java1995;ImportJava.util.Scanner; Public classGame {Privateplayer player; PublicGame (player player) { This. Player =player; } Public voidstart () {System.out.println ("Start the game"); System.out.println (Player.getname ()+ "Enter the game, the initial points are:" +Player.getscore ()); String[] Arr=NewString[] {"Scissors", "stone", "cloth" }; Scanner SC=NewScanner (system.in); while(true) {System.out.println ("Please input scissors, stone, cloth one of them, exit Please enter Exit"); String S1= Sc.next ();//Player's input//handling of input errors if(!filter (S1)) {System.out.println ("The command you entered is incorrect, please re-enter"); Continue; } //game exit mechanism if(S1.equals ("Exit")) { Break; } intCount = (int) (Math.random () * 3);//0.0-2.999 truncationString s2 =Arr[count]; intFlag =rule (S1, S2); System.out.println ("You are out of:" + S1 + ", the system Out is:" +S2); if(Flag > 0) {System.out.println ("Congratulations" + player.getname () + "win, reward 10 points"); Player.setscore (Player.getscore ()+ 10); } Else if(Flag < 0) {System.out.println ("Sorry" + player.getname () + "lost, minus 10 points"); Player.setscore (Player.getscore ()-10); } Else{System.out.println ("It's flat."); } System.out.println (player); } System.out.println (Player.getname ()+ "exited the game, current points are" +Player.getscore ()); } Public Booleanfilter (String s) {if("Scissors". Equals (s) | | "Stone". Equals (s) | | "Cloth". Equals (s) | | "Exit". Equals (s)) { return true; } Else { return false; } } /*** Rules of the game * *@return */ Public intrule (string s1, string s2) {if(s1.equals (S2)) {return0; } if(S1.equals ("scissors"))) { if(S2.equals ("Stone")) { return-1; } if(S2.equals ("cloth")) { return1; } } if(S1.equals ("Stone")) { if(S2.equals ("scissors"))) { return1; } if(S2.equals ("cloth")) { return-1; } } if(S1.equals ("cloth")) { if(S2.equals ("Stone")) { return1; } if(S2.equals ("scissors"))) { return-1; } } return0; }}
Package com.java1995; Public class Test { publicstaticvoid main (string[] args) { new Player ("Li Wei", +) ; New Game (p); G.start (); }}
Resources
[1] Java Easy Start Classic tutorial "full version"
Java 61st Section Stone Scissors cloth mini-game