2 qq 22919713 qq_22919713 于 2016.09.21 13:16 提问

Java初级做小程序。。。。。

图片

3个回答

Marksinoberg
Marksinoberg   Ds   Rxr 2016.09.21 13:53
已采纳

这就是个循环嘛,可以尝试着把循环体里面的内容抽取成一个函数,这样可以锻炼你的抽象问题的能力。仅仅需要对每一轮的猜拳进行判断即可。

guwei4037
guwei4037   Ds   Rxr 2016.09.21 13:19

核心在于出拳,这个可以用Random创建.

比如:((int)Math.Random()*100)%3+1 随机取0-100之间的数,对3取余数,正好是0,1,2 ,再加1,变成1,2,3,代表你的剪刀石头布

xionglangs
xionglangs   Rxr 2016.09.21 22:05

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map map = new HashMap();
map.put(-1, "赢");
map.put(-2, "输");
map.put(0, "打和");
map.put(2, "张飞");
map.put(3, "赵云");
map.put(4, "刘备");
map.put(5, "孙权");
map.put(6, "曹操");
map.put(7, "剪刀");
map.put(8, "石头");
map.put(9, "布");
String answer = "Y";
// 随便写多少
System.out.println("开始猜拳");
System.out.println("猜拳规则");
System.out.print("你选择选择角色");
int meRole = sc.nextInt();
if (meRole == 1) {
meRole = 4;
} else if (meRole == 2) {
meRole = 5;
} else if (meRole == 3) {
meRole = 6;
} else {
System.out.println("你选错了");
}
System.out.println("对方选择角色");
int youRole = sc.nextInt();
System.out.println("需要开始游戏吗");
answer = sc.next().toUpperCase();
while ("Y".equals(answer)) {
int flag = 0;
System.out.println("请出拳.....");
int meFist = sc.nextInt();
if (meFist == 1) {
meFist = 7;
} else if (meFist == 2) {
meFist = 7;
} else if (meFist == 3) {
meFist = 9;
} else {
System.out.println("你出错了,请重新选择");
continue;
}
System.out.println("你出的是" + map.get(meFist));
System.out.println(map.get(youRole) + "出拳");
int youFist = 1 + (int) (Math.random() * 3);
if (youFist == 1) {
youFist = 7;
} else if (youFist == 2) {
youFist = 7;
} else if (youFist == 3) {
youFist = 9;
}
System.out.println(map.get(youRole) + "出的是" + map.get(youFist));
if (meFist == 7 && youFist == 9) {
flag = -1;
} else if (meFist == 7 && youFist == 8) {
flag = -2;
}
if (meFist == 7 && youFist == 9) {
flag = -1;
} else if (meFist == 7 && youFist == 8) {
flag = -2;
}
if (meFist == 7 && youFist == 9) {
flag = -1;
} else if (meFist == 7 && youFist == 8) {
flag = -2;
}
System.out.println("结果是你" + map.get(flag));
System.out.println("需要再次玩吗Y/N");
answer = sc.next().toUpperCase();
}

}
Csdn user default icon
上传中...
上传图片
插入图片