WHCSRL 技术网

JAVA Job Record 2—Rock Paper Scissors (random number)

编写可以玩流行的剪刀石头布游戏的程序(剪刀可以剪布,石头可以砸剪刀,而布可以包石头)
程序提示用户随机产生一个数,这个数为0、1或者2,分别表示石头,剪刀和布。程序提示用户输入值0、1或者2,然后显示一条信息,表明用户和计算机谁赢了游戏,谁输了游戏,或是打成平手。

import java.util.Scanner;

public class Scissorgame{
    public static void main(String[] args){
	    Scanner input =new Scanner(System.in);
	    //声明键盘输入
		int num1 = (int)(Math.random()*3);
		//随机生成0、1、2
		System.out.print("sissor(0),rock(1),paper(2):");
		int num2 = input.nextInt();
		//键盘输入0、1、2
		switch(num2){
			case 0://你剪刀
		        if(num1==0)//电脑剪刀
		        System.out.println("The computer is sissor.You are sissor.It ia a draw"); 
		        else if(num1==1)//电脑石头
				System.out.println("The computer is rock.You are sissor.You lost");
				else //电脑布
				System.out.println("The computer is paper.You are sissor.You won");
				break;
			case 1://你石头                 
                if(num1==1)//电脑石头
		        System.out.println("The computer is rock.You are rock.It ia a draw"); 
				else if(num1==0)//电脑剪刀
		        System.out.println("The computer is sissor.You are rock.You won");
				else //电脑布
		        System.out.println("The computer is paper.You are rock.You lost"); 
				break;
			case 2://你布
		        if(num1==2)//电脑布
		        System.out.println("The computer is paper.You are paper.It ia a draw");
				else if(num1==0)//电脑剪刀
				System.out.println("The computer is sissor.You are paper.You lost");
				else//电脑石头
				System.out.println("The computer is rock.You are paper.You won");
				break;
		}	   		
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在这里插入图片描述

推荐阅读