简单的java猜拳示例
导读:所有的语言实现起来都是类似的import java.util.Scanner; public class caiquan { public static void main(String[] args { Scan...
所有的语言实现起来都是类似的
import java.util.Scanner;
public class caiquan {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] r = {
"石头", "剪刀", "布"}
;
int computerChoice = (int) (Math.random() * 3);
// 随机生成计算机出拳
System.out.println("石头剪刀布游戏开始!");
System.out.println("请出拳:0 - 石头,1 - 剪刀,2 - 布");
int userChoice = scanner.nextInt();
if (userChoice 0 || userChoice >
2) {
System.out.println("无效的输入,请输入0、1或2。");
return;
}
System.out.println("你出了:" + r[userChoice]);
System.out.println("我出了:" + r[computerChoice]);
if (userChoice == computerChoice) {
System.out.println("平局!");
}
else if (userChoice == 0 &
&
computerChoice == 1
|| userChoice == 1 &
&
computerChoice == 2
|| userChoice == 2 &
&
computerChoice == 0) {
System.out.println("你赢了!");
}
else {
System.out.println("你输了!");
}
}
}
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 简单的java猜拳示例
本文地址: https://pptw.com/jishu/7841.html