经过改良,现在写了一个更为高效的Java猜拳游戏,代码如下:
import java.util.*;
public class caiquanxin
{
public static void main(String[] agrs)
{
String first1="拳头",first2="拳头";
Scanner input=new Scanner(System.in);
int computer=(int)(Math.random()*3)+1;
System.out.println("*****猜拳游戏*****");
System.out.println("请出拳(1石头 2剪刀 3布)");
int person=input.nextInt();
switch(person)
{
case 1:first1="石头";
break;
case 2:first1="剪刀";
break;
case 3:first1="布";
break;
}
switch(computer)
{
case 1:first2="石头";
break;
case 2:first2="剪刀";
break;
case 3:first2="布";
break;
}
if(person==computer)
System.out.println("平局,你和电脑都出"+first1);
else if(person==1&&computer==2||person==2&&computer==3||person==3&&computer==1)
System.out.println("你出:"+first1+" 电脑出:"+first2+" 恭喜你,你赢了");
else
System.out.println("你出:"+first1+" 电脑出:"+first2+" 很遗憾,你输了");
}
}