懂java的大神帮我看下这段猜拳游戏错在哪?

2025-04-27 19:01:39
推荐回答(2个)
回答1:

 switch语句中,每个case分支需要添加break语句。

我给你改了下,你看看:

public static void main(String[] args) {
System.out.println("...欢迎来到猜拳游戏。。。 ");
System.out.println("1。石头,2.剪刀,3.布");
System.out.println("...游戏开始请出拳...");
// 人出拳
Scanner in = new Scanner(System.in);
int people = in.nextInt();
switch (people) {
case 1:
System.out.println("你出的是石头");
break;
case 2:
System.out.println("你出的是剪刀");
break;
case 3:
System.out.println("你出的是布");
break;
default:
break;
}
// 电脑随机出拳
int computer = (int) (Math.random() * 3) + 1;
switch (computer) {
case 1:
System.out.println("电脑出的是石头");
break;
case 2:
System.out.println("电脑出的是剪刀");
break;
case 3:
System.out.println("电脑出的是布");
break;
default:
break;
}
// 判断输赢
if (people == computer) {
System.out.println("平局再来一次");
} else if (people == 1 && computer == 3 || people == 2 && computer == 1 || people == 3 && computer == 2) {
System.out.println(" 哈哈你输了");
} else {
System.out.println("恭喜你你赢了");
}
}

回答2:

每个case后面都要加break;