package com.accp;
/**
* 计算机类
*/
public class Computer {
private String name; // 计算机姓名
private int WinTimes; // 计算机赢得次数
/**
* 构造器
*
* @param name
* 计算机的姓名
*/
public Computer(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public int getWinTimes() {
return this.WinTimes;
}
public void setWinTimes(int winTimes) {
this.WinTimes = winTimes;
}
/**
* 计算机出拳的方法
*/
public int showFist() {
return (int) (Math.random() * 3 + 1);
}
}
package com.accp;
import java.util.Scanner;
/**
* 玩家类
*/
public class Player {
private String name; // 玩家姓名
private int WinTimes; // 玩家赢的次数
/**
* 构造器
*
* @param name
* 玩家的姓名
*/
public Player(String name) {
this.name = name;
}
public int getWinTimes() {
return this.WinTimes;
}
public String getName() {
return this.name;
}
public void setWinTimes(int winTimes) {
this.WinTimes = winTimes;
}
/**
* 人出拳的方法
*/
public int showFist() {
int fist;
Scanner sc = new Scanner(System.in);
do {
System.out.println("请出拳:1--剪刀 2--石头 3--布");
System.out.println("请选择: ");
fist = sc.nextInt();
} while (fist < 1 || fist > 3);
return fist;
}
}
package com.accp;
import java.util.Scanner;
/**
* 游戏类
*/
public class Game {
private Computer computer;
private Player player;
private int counter;
public Game(Computer computer, Player player) {
this.computer = computer;
this.player = player;
}
// 显示结果和获胜的次数
public void showReult() {
System.out.println("======比赛结果======");
System.out.println("共比赛了" + counter + "回合");
System.out.println(player.getName() + "赢了" + player.getWinTimes() + "次");
System.out.println(computer.getName() + "赢了" + computer.getWinTimes()
+ "次");
System.out.println("平局 :"
+ (counter - player.getWinTimes() - computer.getWinTimes()));
}
/**
* 是否继续的方法
*/
public void start() {
Scanner sc = new Scanner(System.in);
String s = " ";
do {
play();
System.out.println("\n要继续吗?(输入yes继续)");
s = sc.nextLine();
counter++;
} while (s.substring(0, 1).equalsIgnoreCase("y"));
}
/**
* 判断输赢的方法
*/
public void play() {
int cf = computer.showFist();
int pf = player.showFist();
String[] hj = { "剪刀", "石头", "布" };
System.out.println(computer.getName() + "出了" + hj[cf - 1]);
System.out.println(player.getName() + "出了" + hj[pf - 1]);
if (cf == pf) {
System.out.println("平局!!");
} else if (pf == 1 && cf == 3 || pf == 2 && cf == 1 || pf == 3
&& cf == 2) {
System.out.println(player.getName() + "赢了!!");
player.setWinTimes(player.getWinTimes() + 1);
}
else {
System.out.println(computer.getName() + "赢了!!");
computer.setWinTimes(computer.getWinTimes() + 1);
}
}
}
package com.accp;
/**
* 运行游戏
*/
public class RunGame {
public static void main(String[] args) {
Player p = new Player("曹操");
Computer c = new Computer("刘备");
Game ch = new Game(c, p);
ch.start();
ch.showReult();
}
}
/**
* 计算机类
*/
public class Computer {
private String name; // 计算机姓名
private int WinTimes; // 计算机赢得次数
/**
* 构造器
*
* @param name
* 计算机的姓名
*/
public Computer(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public int getWinTimes() {
return this.WinTimes;
}
public void setWinTimes(int winTimes) {
this.WinTimes = winTimes;
}
/**
* 计算机出拳的方法
*/
public int showFist() {
return (int) (Math.random() * 3 + 1);
}
}
package com.accp;
import java.util.Scanner;
/**
* 玩家类
*/
public class Player {
private String name; // 玩家姓名
private int WinTimes; // 玩家赢的次数
/**
* 构造器
*
* @param name
* 玩家的姓名
*/
public Player(String name) {
this.name = name;
}
public int getWinTimes() {
return this.WinTimes;
}
public String getName() {
return this.name;
}
public void setWinTimes(int winTimes) {
this.WinTimes = winTimes;
}
/**
* 人出拳的方法
*/
public int showFist() {
int fist;
Scanner sc = new Scanner(System.in);
do {
System.out.println("请出拳:1--剪刀 2--石头 3--布");
System.out.println("请选择: ");
fist = sc.nextInt();
} while (fist < 1 || fist > 3);
return fist;
}
}
package com.accp;
import java.util.Scanner;
/**
* 游戏类
*/
public class Game {
private Computer computer;
private Player player;
private int counter;
public Game(Computer computer, Player player) {
this.computer = computer;
this.player = player;
}
// 显示结果和获胜的次数
public void showReult() {
System.out.println("======比赛结果======");
System.out.println("共比赛了" + counter + "回合");
System.out.println(player.getName() + "赢了" + player.getWinTimes() + "次");
System.out.println(computer.getName() + "赢了" + computer.getWinTimes()
+ "次");
System.out.println("平局 :"
+ (counter - player.getWinTimes() - computer.getWinTimes()));
}
/**
* 是否继续的方法
*/
public void start() {
Scanner sc = new Scanner(System.in);
String s = " ";
do {
play();
System.out.println("\n要继续吗?(输入yes继续)");
s = sc.nextLine();
counter++;
} while (s.substring(0, 1).equalsIgnoreCase("y"));
}
/**
* 判断输赢的方法
*/
public void play() {
int cf = computer.showFist();
int pf = player.showFist();
String[] hj = { "剪刀", "石头", "布" };
System.out.println(computer.getName() + "出了" + hj[cf - 1]);
System.out.println(player.getName() + "出了" + hj[pf - 1]);
if (cf == pf) {
System.out.println("平局!!");
} else if (pf == 1 && cf == 3 || pf == 2 && cf == 1 || pf == 3
&& cf == 2) {
System.out.println(player.getName() + "赢了!!");
player.setWinTimes(player.getWinTimes() + 1);
}
else {
System.out.println(computer.getName() + "赢了!!");
computer.setWinTimes(computer.getWinTimes() + 1);
}
}
}
package com.accp;
/**
* 运行游戏
*/
public class RunGame {
public static void main(String[] args) {
Player p = new Player("曹操");
Computer c = new Computer("刘备");
Game ch = new Game(c, p);
ch.start();
ch.showReult();
}
}