1.
public class Test09_02 {
public static void main(String[] args) {
System.out.println("欢迎来到猜拳游戏:t 1:代表剪刀 t 2:代表石头 t 3:代表布");
//记录赢得次数
int num = 0;
//电脑随机生成1-3;
int i = (int) (Math.random()*3+1) ;
System.out.println("电脑出的是"+i);
//判断赢了几句;
while (num<1) {
System.out.println("请输入1-3:");
Scanner input = new Scanner(System.in);
int b = input.nextInt();
//判断用户输入的数是否是1-3
while (b<1 || b>3) {
//不是1-3,重新输入
b = input.nextInt();
} // -1,2输 0平局 2,1 赢了.
int c = b -i;
switch (c) {
case -2:
System.out.println("赢了");
num++;
break;
case -1:
System.out.println("输了请继续");
break;
case 0:
System.out.println("平局请继续");
break;
case 1:
System.out.println("恭喜赢了");
num++;
break;
case 2:
System.out.println("输了继续");
break;
}
}
System.out.println("游戏结束!");
}
2
public class Test09 {
public static void main(String[] args) {
//产生1-3的随机数;
int i = (int)(Math.random()*3+1);
System.out.println(i);
int choose = 0;
//提示用户输入1-3
System.out.println("请输入1-3:nt1:剪刀nt2:石头nt3:布");
Scanner input = new Scanner(System.in);
choose = input.nextInt();
while (choose > 3 || choose <1){ //判断用户输入的数是否在1-3
System.out.println("输入有误!"
+ "请输入1-3:nt1:剪刀nt2:石头nt3:布");
choose = input.nextInt();
} if (choose == i) {
System.out.println("平局!");
} else if (i==1 && choose == 2){
System.out.println("恭喜!你赢了!");
} else if (i==1 && choose == 3) {
System.out.println("你输了!");
} else if (i==2 && choose == 3) {
System.out.println("你输了!");
} else if (i==2 && choose == 1) {
System.out.println("恭喜!你赢了!");
} else if (i==3 && choose == 1) {
System.out.println("你输了!");
} else if (i==3 && choose == 2) {
System.out.println("恭喜!你赢了!");
}
System.out.println("程序结束!");
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)