编写一段锤子剪刀布的java脚本
import java.util.Scanner;
public class HammerScissorsPaper { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int userChoice; int computerChoice = (int)(Math.random() * 3) + 1; String[] options = {"", "锤子", "剪刀", "布"}; String result;
System.out.println("请选择:1. 锤子 2. 剪刀 3. 布");
userChoice = sc.nextInt();
if (userChoice == computerChoice) {
result = "平局";
} else if ((userChoice == 1 && computerChoice == 2) || (userChoice == 2 && computerChoice == 3) || (userChoice == 3 && computerChoice == 1)) {
result = "你赢了";
} else {
result = "计算机赢了";
}
System.out.println("你出了:" + options[userChoice]);
System.out.println("计算机出了:" + options[computerChoice]);
System.out.println(result);
}
}
原文地址: https://www.cveoy.top/t/topic/4oJ 著作权归作者所有。请勿转载和采集!