用Java编写剪刀石头布(方法)
作者:互联网
import java.util.Scanner;
public class text {
public static void main(String[] args) {
int k = 1;
game you = new game();
do {
System.out.println("猜拳游戏现在开始" + "\n" + "输入 0 :石头 1 :剪刀 2 :布");
you.hanshu();
System.out.println("是否继续游戏? 1 : 继续 2 : 关闭");
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
} while (k == 1);
System.out.println("游戏结束你共赢了 " + you.i + " 次");
}
static class game {
int i = 0;
public void hanshu() {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if (n == 0) {
System.out.print("你出石头");
} else if (n == 1) {
System.out.print("你出剪刀");
} else {
System.out.print("你出布");
}
int x = 0 + (int) (Math.random() * 2);
if (x == 0) {
System.out.println("VS电脑出石头");
} else if (x == 1) {
System.out.println("VS电脑出剪刀");
} else {
System.out.println("VS电脑出布");
}
if ((n == 0 && x == 1) || (n == 1 && x == 2) || (n == 2 && x == 0)) {
System.out.println("你赢了");
i++;
} else if (n == x) {
System.out.println("打平");
} else {
System.out.println("电脑赢了");
}
}
}
}
标签:Java,Scanner,int,System,else,println,编写,剪刀,out 来源: https://blog.csdn.net/m0_63788493/article/details/123644303