栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 彩票游戏

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 彩票游戏

题目

Math.random()获取随机数

Math.random()返回的是一个[0.0,1.0)的doule类型的数
所以,获取0-9:(int)Math.random()*10–> [0,10)
获取0-10:(int)Math.random()*10+1–> [0,11)
获取10-99:(int)Math.random()90+10–> [10,100)
公式:
[a,b]: (int)Math.random()
(b-a+1)+a

String类型:使用charAt()获取字符串的字符

由于要每个数字都进行比较,所以使用String类型并使用charAt(i)获取第i个位置上字符串的字符

String ticket=scan.next();
//获取第一和第二个数字
char t1=ticket.charAt(0);
char t2=ticket.charAt(1);

String类型:代码
import java.util.Scanner;
class PlayTicket {
	public static void main(String[] args) {


	//10-99
	String prize=(int)(Math.random()*90+10)+"";
	System.out.println("本期奖金号码为:"+prize);
	char p1=prize.charAt(0);
	char p2=prize.charAt(1);
	Scanner scan=new Scanner(System.in);
	System.out.println("请输入彩票号码:");
	String ticket=scan.next();
	//获取第一和第二个数字
	char t1=ticket.charAt(0);
	char t2=ticket.charAt(1);
		if((t1==p1)&&(t2==p2)){
			System.out.println("奖金10,000美元");
		}else if((t1==p2)&&(t2==p1)){
			System.out.println("奖金3000美元");
		}else if((t1==p1)||(t2==p2)){
			System.out.println("奖金1000美元");
		}else if((t1==p2)||(t2==p1)){
			System.out.println("奖金500美元");
		}else{
			System.out.println("彩票作废");
		}
      
	}
}

Int类型:获取两位数的十位和个位

int ticket=scan.nextInt(); //获取第一和第二个数字
int t1=ticket/10%10;//十位
int t2=ticket/1%10;//个位

Int类型:代码
import java.util.Scanner;
class PlayTicket2 {
	public static void main(String[] args) {
	//使用int类型

	//10-99
	int prize=(int)(Math.random()*90+10);
	System.out.println("本期奖金号码为:"+prize);
	int p1=prize/10%10;//十位
	int p2=prize/1%10;//个位
	Scanner scan=new Scanner(System.in);
	System.out.println("请输入彩票号码:");
	int ticket=scan.nextInt();
	//获取第一和第二个数字
	int t1=ticket/10%10;//十位
	int t2=ticket/1%10;//个位
		if((t1==p1)&&(t2==p2)){
			System.out.println("奖金10,000美元");
		}else if((t1==p2)&&(t2==p1)){
			System.out.println("奖金3000美元");
		}else if((t1==p1)||(t2==p2)){
			System.out.println("奖金1000美元");
		}else if((t1==p2)||(t2==p1)){
			System.out.println("奖金500美元");
		}else{
			System.out.println("彩票作废");
		}
      
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357736.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号