Packagecom.renjicaiquan.entity; Public classComputer {PrivateString name; Private intscore; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetscore () {returnscore; } Public voidSetScore (intscore) { This. score =score; } PublicComputer (String name,intscore) { Super(); This. Name =name; This. score =score; } Publiccomputer () {Super(); } @Override PublicString toString () {return"Computer [name=" + name + ", score=" + score + "]"; } Public intDogame () {intX= (int) (Math.random () *3+1); Switch(x) { Case1: System.out.println ( This. GetName () + "Punch: Scissors"); Break; Case2: System.out.println ( This. GetName () + "Punch: Stone"); Break; Case3: System.out.println ( This. GetName () + "Punch: Cloth"); Break; } returnx; }}
View Code
Packagecom.renjicaiquan.entity;ImportJava.util.Scanner; Public classGame {PrivateUser user=NewUser (); PrivateComputer computer=Newcomputer (); Private intcount; StaticScanner input=NewScanner (system.in); Public voidwelcome () {System.out.println ("-----------------------welcome into the game------------------------"); System.out.println ("\t\t***********************"); System.out.println ("\t\t** scissors, start * * *"); System.out.println ("\t\t***********************"); } Public voidStart () {welcome (); System.out.println ("\ n Punch rule: 1, scissors 2, stone 3, cloth"); System.out.print ("Please select the opponent's role (1: Liu Bei 2: Sun Quan 3: Caocao):"); intcheck_1=Getintonetothree (); Switch(check_1) { Case1: Computer.setname ("Liu Bei"); Break; Case2: Computer.setname ("Sun Quan"); Break; Case3: Computer.setname ("Caocao"); Break; } System.out.print ("Please enter your name:"); User.setname (Input.next ()); System.out.println (User.getname ()+ "\tvs\t" +computer.getname () + "\ n"); System.out.println ("Do you want to start (y/n)?" "); String Answer=Getyorn (); if("N". Equals (Answer)) {System.out.println ("Thanks for using!" "); System.exit (0); } String Isagain=""; Do{ intA=User.dogame (); intb=Computer.dogame (); Compareuserandcomputer (A, b); System.out.print ("\ nyou continue to start the next round (y/n):"); Isagain=Getyorn (); } while(Isagain.equals ("Y")); Showresult (); } Public Static intGetintonetothree () { while(!input.hasnext ("[1,2,3]{1}") ) {input=NewScanner (system.in); System.out.println ("Input is wrong, please re-enter!" "); } returnInput.nextint (); } Public voidsetUser (user user) { This. user =user; } PublicUser GetUser () {returnuser; } Public voidsetcomputer (computer computer) { This. Computer =computer; } PublicComputer Getcomputer () {returncomputer; } Public voidCompareuserandcomputer (intAintb) { //a means a man's punch B means a computer punch. intw=n-b; This. SetCount ( This. GetCount () +1); Switch(x) { Case1: Case-2: User.setscore (User.getscore ()+1); System.out.println ("The result: Congratulations, you won!" "); Break; Case-1: Case2: Computer.setscore (Computer.getscore ()+1); System.out.println ("The result: ^_^, you lost, really stupid!" "); Break; default: System.out.println ("Result: a draw! "); } } Public voidSetCount (intcount) { This. Count =count; } Public intGetCount () {returncount; } Public voidShowresult () {System.out.println ("--------------------------------"); System.out.println (User.getname ()+ "\tvs\t" +computer.getname ()); System.out.println ("PvP Times:" + This. GetCount ()); System.out.println ("\ nthe name \ t Score"); System.out.println (User.getname ()+ "\ T" +User.getscore ()); System.out.println (Computer.getname ()+ "\ T" +computer.getscore () + "\ n"); if(User.getscore () >Computer.getscore ()) {System.out.println (Computer.getname ()+ ": This is impossible! "); }Else if(User.getscore () = =Computer.getscore ()) {System.out.println (Computer.getname ()+ ": Fight 300 rounds again! "); }Else{System.out.println (Computer.getname ()+ ": I" +computer.getname () + "is invincible, hahaha ..."); } System.out.println ("--------------------------------"); } Public StaticString Getyorn () { while(!input.hasnext ("[Y,n]{1}") ) {input=NewScanner (system.in); System.out.println ("Input is wrong, please re-enter!" "); } returnInput.next (); }}
View Code
Packagecom.renjicaiquan.entity; Public classUser {PrivateString name; Private intscore; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetscore () {returnscore; } Public voidSetScore (intscore) { This. score =score; } PublicUser (String name,intscore) { Super(); This. Name =name; This. score =score; } PublicUser () {Super(); } @Override PublicString toString () {return"User [name=" + name + ", score=" + score + "]"; } Public intDogame () {System.out.println ("You Punch out:"); intx=Game.getintonetothree (); Switch(x) { Case1: System.out.println ("You punch: Scissors."); Break; Case2: System.out.println ("You Punch: Stone"); Break; Case3: System.out.println ("You punch: Cloth"); Break; } returnx; } }
View Code
Package com.renjicaiquan.test; Import Com.renjicaiquan.entity.Game; Public class Testgame { publicstaticvoid main (string[] args) { Game g= New Game (); G.start (); }}
View Code
Scissors and stone cloth