/**
* @ClassName TestDemo1
* @Description 石头剪刀布
* @Author LiMingXu
* @Date 2018/11/5 0:44
* @Version 1.0
**/
public class TestDemo1 {
public static class People{
private String name;
private int score;
public People(){
}
public People(String name){
score = 0;
this.name = name;
}
public String fist(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:石头 剪刀 布");
String str = scanner.next();
return str;
}
public void addScore(){
score += 1;
}
public int getScore(){
return score;
}
public String getName(){
return name;
}
}
///
static class Computer {
private String name;
private int score;
public Computer(String name) {
score = 0;
this.name = name;
}
public String fist() {
Random random = new Random(3);
int n = random.nextInt() + 1;//[1,3]
String str = null;
switch (n) {
case 1:
System.out.print(name + "出石头");str = "石头";break;
case 2:
System.out.print(name + "对方出剪刀");str = "剪刀";
break;
case 3:
System.out.print(name + "对方出布");str = "布";
break;
}
return str;
}
public void addScore() {
score += 1;
}
public int getScore() {
return score;
}
public String getName(){
return name;
}
}
public static class Game{
private People people;
private Computer computer;
private Game(){
People people= new People("Tyone");
Computer computer= new Computer("Alpha");
}
public Game(People people,Computer computer){
this.computer=computer;
this.people=people;
}
private void playThreeTime(){
int count = 0;
while(count < 3){
String pFist = people.fist();
String cFist = computer.fist();
if(pFist.equals("石头") && cFist.equals("剪刀") || pFist.equals("剪刀") && cFist.equals("布")||pFist.equals("布") && cFist.equals("石头")){
System.out.println(people.getName() + "本局获胜");
people.addScore();
}else if(pFist.equals(cFist)){
System.out.println("平局!");
}else{
System.out.println(computer.getName() + "本局获胜");
computer.addScore();
}
count ++;
}
}
private void getResult(){
int pScore = people.getScore();
int cScore = computer.getScore();
if(pScore > cScore){
System.out.println(people.getName()+ "最终结果赢了");
}else if(pScore == cScore) {
System.out.println("平局");
}else{
System.out.println(computer.getName() + "最终结果赢了");
}
System.out.println(people.getName()+":"+computer.getName()+"="+people.getScore()+":"+computer.getScore);
}
public void start(){
while(true){
playThreeTime();
getResult();
System.out.println("是否继续?");
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
if(str.equals("是")){
continue;
}else{
break;
}
}
}
}
public static void main(String[] args) {
People people= new People("Tyone");
Computer computer = new Computer("Alpha");
Game game = new Game(people,computer);
game.start();
}
}
Java-石头剪刀布
猜你喜欢
转载自blog.csdn.net/lmx9813/article/details/83789681
今日推荐
周排行