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

package 完美;
import java.util.*;
public class txt9 {
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("你出的是石头");
case 2:
System.out.println("你出的是剪刀");
case 3:
System.out.println("你出的是布");
}
//电脑随机出拳
int computer=(int)(Math.random()*3)+1;
switch(computer){
case 1:
System.out.println("电脑出的是石头");
case 2:
System.out.println("电脑出的是剪刀");
case 3:
System.out.println("电脑出的是布");
}
//判断输赢
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("恭喜你你赢了");

}

}
}
我运行的结果是为什么是乱的想不通请指导
...欢迎来到猜拳游戏。。。
1。石头,2.剪刀,3.布
...游戏开始请出拳...
2
你出的是剪刀
你出的是布
电脑出的是布
恭喜你你赢了

 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("恭喜你你赢了");
}
}

追问

谢谢这位大神,不过为什么忘写break,MyEclipse没有提示错误?

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-25
每个case后面都要加break;
相似回答