多分支练习石头剪刀布

代码如下:

/*
  多分支练习石头剪刀布
  随机生成一个0/1/2的数字分别代表石头/剪刀/布
  提示用户输入0/1/2,判断输赢
*/
import java.util.Scanner;
class Demo14 
{
public static void main(String[] args) 
{
//随机生成
int computer=(int)(Math.random()*3);
//提示用户输入
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个0/1/2的数字分别代表石头/剪刀/布");
int person=sc.nextInt();
switch(computer){
 case 0:System.out.println("计算机出的石头");break;
 case 1:System.out.println("计算机出的剪刀");break;
 case 2:System.out.println("计算机出的布");break;
}
switch(person){
 case 0:System.out.println("计算机出的石头");break;
 case 1:System.out.println("计算机出的剪刀");break;
 case 2:System.out.println("计算机出的布");break;
}
//判断输赢
if((computer==0&&person==1)||(computer==1&&person==2)||(computer==2&&person==0))
//计算机赢
System.out.println("计算机赢");
else if((computer==0&&person==2)||(computer==1&&person==0)||(computer==2&&person==1))
//人赢
System.out.println("人赢");
else
//平局
System.out.println("平局");
}
}

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...