public class Computer {
String name = "Computer";//Name
int score = 0;//integral
public int showfist () {
Generate random numbers
int show = (int) ((Math.random () *10)%3+1); Generate a random number to indicate a computer punch
Output punch result and return
Switch (show) {
Case 1:
System.out.println (name+ "Punch: Scissors");
Break
Case 2:
System.out.println (name+ "punch: Stone");
Break
Case 3:
System.out.println (name+ "punch: cloth");
Break
}
return show;
}
public class Person {
String name = "Anonymous"; Name
int score = 0; Integral
public int showfist () {
Receive User's Choice
Scanner input = new Scanner (system.in);
System.out.print ("\ n please punch (1. Scissors 2. Stone 3. Cloth):");
int show = Input.nextint ();
Output punch result, and return
Switch (show) {
Case 1:
System.out.println ("You Punch: Scissors");
Break
Case 2:
System.out.println ("You Punch: Stone");
Break
Case 3:
System.out.println ("You Punch: cloth");
Break
}
return show;
}
Import Java.util.Scanner;
public class Game {
person person; User
Computer computer;//man-machine
int count;//PvP Times
public void inital () {
person = new person ();
Computer = new computer ();
Count = 0;
}
Start the game
public void Startgame () {
SYSTEM.OUT.PRINTLN ("-------------welcome into Scissors Game---------------");
System.out.println ();
System.out.println ("******************************************");
SYSTEM.OUT.PRINTLN ("* * scissors, start * *");
System.out.println ("******************************************");
System.out.println ();
System.out.println ("Punching rule: 1. Scissors 2. Stone 3. Cloth");
Scanner input = new Scanner (system.in);
String exit= "n";//Exit system
do {
Inital ();//initialization
Select opponent
System.out.print ("Choose Your Opponent" (1. Liu Bei 2. Sun Quan 3. Cao): ");
int role = Input.nextint ();
Switch (role) {
Case 1:
Computer.name= "Liu Bei";
SYSTEM.OUT.PRINTLN ("You have chosen" + computer.name+ "PvP");
Break
Case 2:
Computer.name= "Sun Quan";
SYSTEM.OUT.PRINTLN ("You have Chosen" +computer.name+ "PvP");
Break
Case 3:
SYSTEM.OUT.PRINTLN ("You have Chosen" +computer.name+ "PvP");
Break
Default
SYSTEM.OUT.PRINTLN ("Input Error! ");
Break
}
Enter the name of the game player
System.out.print ("Please enter your name:");
Person.name=input.next ();
System.out.println (person.name+ "VS" +computer.name+ "PVP \ n");
Start the game
System.out.print ("Do you want to start?" ( y/n) ");
Start a game
String start = Input.next ();
int perfist;//the user to punch
int comfist;//computer out fist
while (Start.equals ("Y")) {
Punch out
Perfist = Person.showfist ();
Comfist = Computer.showfist ();
Judging the winner
if ((perfist==1&&comfist==1) | | | (perfist==2&&comfist==2) | | (perfist==3&&comfist==3)) {
SYSTEM.OUT.PRINTLN ("Result: Draw, Bad!\n");//Tie
}else if ((perfist==2&&comfist==1) | | (perfist==3&&comfist==2) | | (perfist==1&&comfist==3)) {
SYSTEM.OUT.PRINTLN ("The result says: ^_^, you lose, really stupid!\n");//Computer wins
computer.score++;
}else {
SYSTEM.OUT.PRINTLN ("Result: Congratulations, you won the!\n");//Victory
person.score++;
}
count++;
System.out.print ("Whether to proceed to the next round (y/n)");
Start = Input.next ();
}
Show results
Showresult ();
System.out.print ("\ nyou want to start the next game?" ( y/n): ");
Exit = Input.next ();
System.out.println ();
} while (!exit.equals ("n"));
SYSTEM.OUT.PRINTLN ("System exit");
}
private void Showresult () {
Show PvP Times
System.out.println ("------------------------------");
System.out.println (computer.name+ "VS" +person.name);
System.out.println ("PvP Times:" +count);
Show Final Score
System.out.println ("\ n name \ t score");
System.out.println (person.name+ "\ t" +person.score);
System.out.println (computer.name+ "\ t" +computer.score+ "\ n");
Show PVP Results
int result = 3;
if (result==1) {
SYSTEM.OUT.PRINTLN ("Result: a tie, a real loser!");
}else if (result==2) {
SYSTEM.OUT.PRINTLN ("Result: Congratulations, you won!");
}else
{
SYSTEM.OUT.PRINTLN ("Result: Oh, stupid, next refueling ah!"); /Computer wins
}
System.out.println ("--------------------------");
}
Calculate Game Results
private int Calcresult () {
TODO auto-generated Method Stub
if (Person.score = = Computer.score) {
Return 1;//Battle Flat
}else if (Person.score > Computer.score) {
Return 2;//game player wins
}else {
Return 3;//defeat
}
}
}
public class Test {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Game game = new Game ();
Game.inital ();
Game.startgame ();
}
}
}
}
13th case of human-machine scissors