Java猜拳游戏和Random的应用

关于猜拳游戏 

package com.koi.shangke.lianxi;
/**
 * 
 *用流程控制实现猜拳游戏
 *
 */
import java.util.Scanner;
import java.util.Random;
public class Demo_2 {

	public static void main(String[] args) {
	System.out.println("*******欢迎来到猜拳游戏******");	
	System.out.println("游戏三局两胜,如果获胜会得到精美小礼品");
	System.out.println("游戏规则:从键盘中输入1,2,3中任意一个数");
	System.out.println("1-石头\t2-剪刀\t2-布");
	Scanner scan=new Scanner(System.in);
	int count1=0;
	int count2=0;
	int count3=0;
	while(true){
	
	int people=scan.nextInt();
	
		System.out.println("我们的游戏要开始了,请输入一个数");
	switch(people){
	case 1:
		System.out.println("您出的是石头");
		break;
	case 2:
		System.out.println("您出的是剪刀");
		break;
	case 3:
		System.out.println("您出的是布");
		break;
	}
	Random ram=new Random();
	int computer=ram.nextInt(3)+1;
	switch (computer){
	case 1:
		System.out.println("电脑:石头");
		break;
	case 2:
		System.out.println("电脑:剪刀");
		break;
	case 3:
		System.out.println("电脑:布");
		break;
	}
	if(people==computer){
		count3++;
		System.out.println("平局");
	}
	else if(people==1&&computer==2||people==2&&computer==3||people==3&&computer==1){
		count1++;
		System.out.println("本局胜利");
	}else{
		count2++;
		System.out.println("本局输了");
	}
	System.out.println("请重新输入");
	if(count1+count2+count3==3){
		System.out.println("*******游戏结束*******");
	
		if(count1>count2){
			System.out.println("******恭喜你获得最终的胜利******");
		}else{
			System.out.println("*******好遗憾啊******");
			
		}
	
		
	
		
		break;
	}
	
	}
	scan.close();
	}
}
	
	

关于Random的应用

1.产生随机数

        Math.random():产生0.0-1.0(不包括1.0)的double类型的数据。

         例如:0-100

        int a=(int)(Math.random()*100+1;

2.产生a-b之间的整数(a<b)

int c=(int)(Math.random()*(a-b)+a);

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值