模拟石头剪刀布游戏
import java.util.Scanner;
public class Sjb {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“你出什么? (石头(2)、剪刀(1)、布(0)”);//获取用户输入的数
int sjb = sc.nextInt();
int computer =(int)( Math.random()*3);与电脑随机数比较 Math.random()这个方法表示的是(0,1)
if (computer == sjb) { //computer输出的是0 1 2
System.out.println(“平手 再来一局”);
}
else if (computer - sjb == 1 || computer - sjb == -2 || sjb - computer == 2 || sjb - computer == -1)
{
System.out.println(“你输入的是 :”+sjb+" 电脑输入的是 :"+computer+" (石头(2)、剪刀(1)、布(0)") : 电脑获胜");
}
else if (computer - sjb == 2 || computer - sjb == -1 || sjb - computer == 1 || sjb - computer == -2)
{
System.out.println(“你输入的是 :”+sjb+" 电脑输入的是 :"+computer+" (石头(2)、剪刀(1)、布(0)") : 恭喜你赢了");
}
}
}