ImportJava.util.Scanner;PublicClassperson {Scanner input =NewScanner (system.in);String name;IntScore; String Action;IntNum PublicvoidMethod () {System.out.println ("\ n please punch: 1. Scissors 2. Stone 3. Cloth");Boolean a =True;Do{num =Input.nextint ();if (num = = 1 | | num = 2 | | num = = 3) {Switch(num) {Case 1: Action = "Scissors" ; break; case 2: action = "stone" break; case 3: action = "cloth" break;} A = false; System.out.println ("You punch:" + action);} else {System.out.println ("input number is wrong, please reenter" ); }} while (a);}
Second class, Computer class:
PublicClassComputer {String name;IntScore; String Action;IntNum PublicvoidMethod () { num= (int) ((Math.random ( ) +1switchcase 1: action= "Scissors" ; break; case 2: action= "stone" break; case 3: action= "cloth" break;} System.out.println (name+ "Punch:" +action);}}
Third class, game class:
ImportJava.util.Scanner;PublicClassGame {Scanner input =NewScanner (system.in);Person person =New Person ();//Create an object of the Player class computer computer =New computer ();//Creating objects for a computer classIntNumberint frequency = 0; PublicvoidProcess () {System.out. println ("--------------------------Welcome to 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 role (1: Liu Bei 2: Sun Quan 3: Caocao):");Boolean B =True;Do{Number =Input.nextint ();if (number = = 1 | | number = = 2 | | number = = 3) {Switch(number) {Case 1: Computer.name = "Liu Bei";Break;Case 2: Computer.name = "Sun Quan";Break;Case 3: Computer.name = "Caocao";Break; } B =False; }Else{System.out.println ("Enter the wrong number, 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) ");Char answer = Input.next (). CharAt (0);while (answer = = ' Y ') {Person.method (); Computer.method ();if (Person.num = =Computer.num) {System.out.println ("hehe, draw, wait and see!") "); }Elseif ((Person.num = = 2) && (Computer.num = = 1)) || (Person.num = = 1) && (Computer.num = = 3) || ((Person.num = = 3) && (Computer.num = = 2) {System.out.println ("Wow, you win, it's awesome!") "); 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); } } PublicvoidShowresult () {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" Person.score); System.out.println (computer.name + "\t\t" + Computer.score); if (person.score < Computer.score) { System.out.println ("hehe, 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:
Class Demo {void main (string[] args) {game play=new Game (); Create a Game class object Play.process (); // Call Game Game Process Method Play.showresult (); call the game class's Settlement method }}
Man-Machine Scissors