此程序精华在于判断非平局的算法我自己进行了优化(else if ((human_opt - comp_opt + 1) % 3 == 0) ),使得代码少而精。
出发点在于我看到非平局时双方取值总是首尾相连,因此始终给任一方加权(如减去1),则可以用取模的思想来实现判断
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// 假设1代表石头,2代表剪刀,3代表布。
// 计算机随机出拳,10次结束
int count;
int human_win = 0;
int comp_win = 0;
int equal = 0;
String[] state = { "石头", "剪刀", "布" };
Scanner input = new Scanner(System.in);
for (count = 1; count < 11; count++) {
System.out.println("第" + count + "局开始,请出拳:(1代表石头,2代表剪刀,3代表布)");
int human_opt = input.nextInt();
int comp_opt = (int) (Math.random() * 3 + 1);
if (human_opt > 3 | human_opt < 1) {
System.out.println("乱出拳,流局,重新出拳");
cou