JavaHomeWorkList

3.17 关键词:剪刀石头布;随机数

1 import java.util.Scanner; 2 public class JSB { 3 public static void main(String[] args) { 4 int a = (int)(Math.random() * 3); 5 Scanner input = new Scanner(System.in); 6 System.out.println("输入0(剪刀)/1(石头)/2(布):"); 7 int x = input.nextInt(); 8 9 if (x != 0 && x != 1 && x != 2) 10 System.out.println("请输入0/1/2!"); 11 else { 12 if (a == 0 && x == 2) 13 System.out.println("电脑是剪刀,你是布,电脑赢!"); 14 if (a == 1 && x == 0) 15 System.out.println("电脑是石头,你是剪刀,电脑赢!"); 16 if (a == 2 && x == 1) 17 System.out.println("电脑是布,你是石头,电脑赢!"); 18 if (a == 0 && x == 1) 19 System.out.println("电脑是剪刀,你是石头,你赢!"); 20 if (a == 1 && x == 2) 21 System.out.println("电脑是石头,你是布,你赢!"); 22 if (a == 2 && x == 0) 23 System.out.println("电脑是布,你是剪刀,你赢!"); 24 if (a == x) 25 System.out.println("平手!"); 26 System.out.println("电脑是:" + a); 27 } 28 input.close(); 29 } 30 31 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyjwxx.html