package cn.itcast_01; |
import java.util.Random; |
import java.util.Scanner; |
/* |
* 对猜拳游戏的优化 |
* |
*/ |
public class GuessGame2 { |
public static void main(String[] args) { |
while ( true ){ |
//键盘录入 |
System.out.println( "0:剪刀、1:石头、2:布 、3:退出游戏 请输入你的数字:" ); |
Scanner sc = new Scanner(System.in); |
int number = sc.nextInt(); |
|
//产生0--2随机数 |
Random rd = new Random(); |
int pcnumber = rd.nextInt( 3 ); |
|
//判断是否退出 |
if (number == 3 ){ |
System.out.println( "欢迎再来!!!" ); |
break ; |
} |
|
//比较并输出结果 |
switch (number) { |
case 0 : |
{ |
if (pcnumber == 0 ){ |
System.out.println( "你出:剪刀 电脑出:剪刀" ); |
System.out.println( "平局" ); |
} else if (pcnumber == 1 ){ |
System.out.println( "你出:剪刀 电脑出:石头" ); |
System.out.println( "你输了" ); |
} else { |
System.out.println( "你出:剪刀 电脑出:布" ); |
System.out.println( "你赢了" ); |
|
} |
|
} |
break ; |
case 1 : |
{ |
if (pcnumber == 0 ){ |
System.out.println( "你出:石头 电脑出:剪刀" ); |
System.out.println( "你赢了" ); |
} else if (pcnumber == 1 ){ |
System.out.println( "你出:石头 电脑出:石头" ); |
System.out.println( "平局" ); |
} else { |
System.out.println( "你出:石头 电脑出:布" ); |
System.out.println( "你输了" ); |
} |
|
} |
|
break ; |
case 2 : |
{ |
if (pcnumber == 0 ){ |
System.out.println( "你出:布 电脑出:剪刀" ); |
System.out.println( "你输了" ); |
} else if (pcnumber == 1 ){ |
System.out.println( "你出:布 电脑出:石头" ); |
System.out.println( "你赢了" ); |
} else { |
System.out.println( "你出:布 电脑出:布" ); |
System.out.println( "平局" ); |
} |
|
} |
|
break ; |
default : |
System.out.println( "您输入有误,请按提示输入!!!" ); |
break ; |
} |
|
|
} |
|
} |
} |
by: 发表于:2017-12-28 14:04:48 顶(0) | 踩(0) 回复
??
回复评论