猜拳游戏(Java实现)
import java.util.Random;
import java.util.Scanner;
public class MoraGame {
    public static void main(String[] args) {
        int humanNum = 0;
        int n = 0;
        int i = 0;
        int j;
        int k;
        //保存对战信息
        int[][] Combat = new int[3][3];
        //保存输赢情况
        String[] infoArr = new String[3];
        Scanner scanner = new Scanner(System.in);
        Caiquan c1 = new Caiquan();
        while (n < 3) {
            n++;
            System.out.println("请输入你要出的拳(0-拳头 1-剪刀 2-布:)");
            if (scanner.hasNext()) {
                humanNum = scanner.nextInt();
            }
            c1.computerNum();
            c1.sethumanNum(humanNum);
            int res = c1.vsComputer();
            String notice = c1.winorlose(res);
            System.out.println("==========================================================");
            System.out.println("局数\t玩家的出拳\t电脑的出拳\t输赢情况");
            System.out.println(c1.totalcount + "\t" + c1.humanNum + "\t\t" + c1.comNum + "\t\t" + notice + "\n");
            infoArr[i] = notice;
            Combat[i][0] = c1.totalcount;
            Combat[i][1] = c1.humanNum;
            Combat[i][2] = c1.comNum;
            i++;
        }
        System.out.println("局数\t玩家的出拳\t电脑的出拳\t输赢情况");
        for (j = 0; j < Combat.length; j++) {
            for(k = 0; k < Combat[j].length; k++) {
                System.out.print(Combat[j][k] + "\t\t");
            }
            System.out.println(infoArr[j]);
        }
        System.out.println("你赢了" + c1.wincount + "次");   
    }
}
class Caiquan {
    int humanNum;
    int comNum;
    int wincount;
    int losecount;
    int tiecount;
    int totalcount;
    public void sethumanNum(int humanNum) {
        this.humanNum = humanNum;
    }
    public int computerNum() {
        Random r = new Random();
        this.comNum = r.nextInt(3);
        return this.comNum;
    }
    public int vsComputer() {
        //0-拳头 1-剪刀 2-布
        //返回值:0 赢 -1 输  2 平局 -2异常
        if (this.humanNum == 0 && this.comNum == 1) {
            return 0;
        }
        else if (this.humanNum == 0 && this.comNum == 2) {
            return -1;
        }
        else if (this.humanNum == 0 && this.comNum == 0) {
            return 2;
        }
        else if (this.humanNum == 1 && this.comNum == 0) {
            return -1;
        }
        else if (this.humanNum == 1 && this.comNum == 2) {
            return 0;
        }
        else if (this.humanNum == 1 && this.comNum == 1) {
            return 2;
        }
        else if (this.humanNum == 2 && this.comNum == 0) {
            return 0;
        }
        else if (this.humanNum == 2 && this.comNum == 1) {
            return -1;
        }
        else if (this.humanNum == 2 && this.comNum == 2) {
            return 2;
        }
        else return -2;
    }
    public String winorlose(int res) {
        //0 赢 -1 输  2 平局
        if (res == 0) {
            this.wincount++;
            this.totalcount++;
            return "你赢了";
        }
        else if (res == -1) {
            this.losecount++;
            this.totalcount++;
            return "你输了";
        }
        else if (res == 2) {
            this.tiecount++;
            this.totalcount++;
            return "平局了";
        }
        else
            this.totalcount++;
            return "异常!";
    }
}
运行示例:
输入你要出的拳(0-拳头 1-剪刀 2-布:)
1
==========================================================*
局数    玩家的出拳      电脑的出拳      输赢情况
1       1               0               你输了
请输入你要出的拳(0-拳头 1-剪刀 2-布:)
2
==========================================================*
局数    玩家的出拳      电脑的出拳      输赢情况
2       2               0               你赢了
请输入你要出的拳(0-拳头 1-剪刀 2-布:)
0
==========================================================*
局数    玩家的出拳      电脑的出拳      输赢情况
3       0               2               你输了
局数    玩家的出拳      电脑的出拳      输赢情况
1               1               0               你输了
2               2               0               你赢了
3               0               2               你输了
你赢了1次

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号