一个简单的猜拳游戏标签:JAVA,猜拳,computerGuess,int,System,游戏,peopleGuess,println,out From: https://www.cnblogs.com/wase/p/17134194.html
package com.zhou.java.demo02;
import java.util.Random;
import java.util.Scanner;
public class Demo09 {
public static void main(String[] args) {
People people = new People();
int WinCount = 0;
int[][] arr = new int[3][3];
int j = 0;
String[] arr2 = new String[3];
Scanner sc = new Scanner(System.in);
String isWin ;
for (int i = 0; i < 3; i++) {
System.out.println("请输入你要猜的拳 (0代表拳头,1代表剪刀,2代表布)");
int num = sc.nextInt();
// 定义人出拳
people.setPeopleGuess(num);
int tomGuess = people.getPeopleGuess();
arr[i][j + 1] = tomGuess;
//定义电脑出拳
int comGuess = people.computerGuess();
arr[i][j + 2] = comGuess;
// 比赛情况
isWin = people.vsComputer();
arr2[i] = isWin;
arr[i][j] = people.count;
//输出比赛情况
System.out.println("================================");
System.out.println("局数\t\t玩家的出拳\t电脑的出拳\t输赢情况");
System.out.println(people.count + " " + tomGuess + "\t " + comGuess + "\t " + isWin);
System.out.println("================================");
System.out.println("\n\n");
WinCount = people.count(isWin);
}
//总结比赛情况
System.out.println("局数\t玩家出拳\t电脑出拳\t输赢情况");
for (int i = 0; i < arr.length; i++) {
for (int k = 0; k < arr[i].length; k++) {
System.out.print(arr[i][k] + " ");
}
System.out.println(arr2[i]);
System.out.println();
}
System.out.println("你赢了" + WinCount + "次");
if(WinCount>=2){
System.out.println("恭喜你,三次比赛你胜过了电脑!");
}else {
System.out.println("很遗憾,你失败了!");
}
}
}
class People{
int peopleGuess;
int computerGuess;
int winCount = 0;
int count =1;
// 电脑 猜拳
public int computerGuess(){
Random r =new Random();
computerGuess = r.nextInt(3);
return computerGuess;
}
// 人 猜拳
public void setPeopleGuess(int peopleGuess){
if (peopleGuess >2 ||peopleGuess<0){
throw new IllegalArgumentException("输入错误");
}
this.peopleGuess =peopleGuess;
}
public int getPeopleGuess(){
return peopleGuess;
}
// 定义输赢情况
public String vsComputer(){
if (peopleGuess==0&&computerGuess==1){
return "你赢了";
}else if (peopleGuess==1&&computerGuess==2){
return "你赢了";
}else if(peopleGuess==2&&computerGuess==0){
return "你赢了";
}else if(peopleGuess==computerGuess){
return "平局";
}else {
return "你输了";
}
}
// 定义赢得次数
public int count(String s){
count++;
if (s.equals("你赢了")){
winCount++;
}
return winCount;
}
}