当前位置:网站首页>双色球案例
双色球案例
2022-07-04 06:11:00 【张 明明】
p69任务描述如下
感觉比写数据结构的c代码简单不少!!
希望10天入门吧!
package com.itheima.practice;
import java.util.Random;
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
//生成的随机幸运数
int[] Lucknumber=creatLuckNumber();
//用户输入的选号
int[] Usernumber=userInputNumer();
//判断中奖情况
judge(Lucknumber,Usernumber);
}
//生成幸运数字的方法
public static int[] creatLuckNumber(){
//定义一个动态数组,存储7个数字
int[] numbers=new int[7];
//遍历数组,生成7个幸运号码
//前六个为红色球(1-33)不能重复,最后一个蓝色球(1-16)
Random r =new Random();
for (int i = 0; i < 7; i++) {
while(true){
//生成一个1-33的随机数
int data=r.nextInt(33)+1;
boolean flag=true;
//检查是否重复
for (int j = 0; j < i; j++) {
if(numbers[i]==data){
flag=false;
break;
}
}
if(flag){
numbers[i]=data;
break;
}
}
}
//为蓝色球生成1-16的随机数
numbers[6]=r.nextInt(16)+1;
return numbers;
}
//用户输入选号的方法
public static int[] userInputNumer(){
int[] numbers=new int[7];
Scanner sc=new Scanner(System.in);
for (int i = 0; i < numbers.length-1; i++) {
System.out.println("请你输入第"+(i+1)+"红色号码(1-33)");
int data=sc.nextInt();
numbers[i]=data;
}
System.out.println("请你输入蓝色号码(1-16)");
numbers[6]=sc.nextInt();
return numbers;
}
public static void judge(int[] Lucknumber,int[] usernumber) {
int redHit=0;
int blueHit=0;
//判断红色球命中几个
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if(Lucknumber[j]==usernumber[i]){
redHit++;
break;
}
}
}
//判断最后一个是否命中
if(Lucknumber[6]==usernumber[6]) blueHit++;
//显示中奖详情
System.out.print("幸运号码是:");
printarray(Lucknumber);
System.out.println("你选的号码是:");
printarray(usernumber);
System.out.println("红色球中了"+redHit+"个,蓝色球中了"+blueHit+"个");
//判断中奖情况
if(blueHit==1&&redHit<3) System.out.println("恭喜你中了5元");
else if(blueHit==1&&redHit==3||blueHit==0&&redHit==4) System.out.println("恭喜你中了10元");
else if(blueHit==1&&redHit==4||blueHit==0&&redHit==5) System.out.println("恭喜你中了200元");
else if(blueHit==1&&redHit==5) System.out.println("恭喜你中了3000元");
else if(blueHit==0&&redHit==6) System.out.println("恭喜你中了500万元");
else if(blueHit==1&&redHit==6) System.out.println("恭喜你中了1000万元");
else System.out.println("很遗憾没有中奖");
}
public static void printarray(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i== arr.length?arr[i]:arr[i]+",");
}
System.out.println("]");
}
}
结果
没办法,我就是天选之人!!!
边栏推荐
- js获取对象中嵌套的属性值
- 如何避免 JVM 内存泄漏?
- Design and implementation of redis 7.0 multi part AOF
- 4G wireless all network solar hydrological equipment power monitoring system bms110
- Fast power (template)
- Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
- [microservice] Nacos cluster building and loading file configuration
- 《ClickHouse原理解析与应用实践》读书笔记(4)
- Distributed cap theory
- 每周小结(*63):关于正能量
猜你喜欢
如何实现视频平台会员多账号登录
QT 获取随机颜色值设置label背景色 代码
C语言练习题(递归)
Detectron: train your own data set -- convert your own data format to coco format
buuctf-pwn write-ups (8)
检漏继电器JY82-2P
Compound nonlinear feedback control (2)
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
Arc135 C (the proof is not very clear)
input显示当前选择的图片
随机推荐
How to clone objects
input显示当前选择的图片
How to implement lazy loading in El select (with search function)
Experience weekly report no. 102 (July 4, 2022)
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
Arc135 a (time complexity analysis)
left_ and_ right_ Net interpretable design
Impact relay jc-7/11/dc110v
Take you to quickly learn how to use qsort and simulate qsort
MySQL的information_schema数据库
Accidentally deleted the data file of Clickhouse, can it be restored?
Average two numbers
报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
检漏继电器JY82-2P
冲击继电器JC-7/11/DC110V
Manually page the list (parameter list, current page, page size)
复合非线性反馈控制(二)
Invalid revision: 3.18.1-g262b901-dirty
C实现贪吃蛇小游戏