package Caiquan; import java.util.Scanner; public class Caiquan { public static void main(String[] args) { int c_score = 0, p_score = 0; System.out.println("请设置你需要玩的次数"); int c = new Scanner(System.in).nextInt(); for (int i = 0; i < c; i++) { Caiquan caiquan = new Caiquan(); int a = caiquan.Person(); int b = caiquan.Computer(); if (a == b) { System.out.println("双方都未得分\n"); } else if ((a == 0 && b == 1 || b == 2) || (a == 1 && b == 2)) { System.out.println("你赢了\n"); p_score++; } else if ((b == 0 && a == 1 || a == 2) || (b == 1 && a == 2)) { System.out.println("你输了\n"); c_score++; } } System.out.println("共进行了" + c + " 局游戏"); if (p_score == c_score) { System.out.println("双方打成平局"); } if (p_score > c_score) { System.out.println("你获得了" + p_score + "分,获得最终胜利"); } if (p_score < c_score) System.out.println("你输了,下次继续努力"); } int Computer() { int num = (int) (Math.random() * 3); switch (num) { case 0: System.out.println("电脑出的是剪刀"); break; case 1: System.out.println("电脑出的是石头"); break; case 2: System.out.println("电脑出的是布"); break; } return num; } int Person() { System.out.printf("请输入0代表剪刀,1代表石头,2代表布\n"); int num = new Scanner(System.in).nextInt(); if (num >= 0 && num <= 2) { switch (num) { case 0: System.out.printf("你输入的是剪刀\n"); break; case 1: System.out.printf("你输入的是石头\n"); break; case 2: System.out.printf("你输入的是布\n"); break; } } return num; } }
练习 剪刀石头布
最新推荐文章于 2024-11-08 23:31:31 发布