当前位置:网站首页>双色球案例
双色球案例
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("]");
}
}
结果
没办法,我就是天选之人!!!
边栏推荐
- 【无标题】
- Uninstall Google drive hard drive - you must exit the program to uninstall
- ABAP:OOALV实现增删改查功能
- 我的NVIDIA开发者之旅——优化显卡性能
- Nexus 6p downgraded from 8.0 to 6.0+root
- 509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
- How to solve the component conflicts caused by scrollbars in GridView
- 如何展开Collapse 的所有折叠面板
- Accidentally deleted the data file of Clickhouse, can it be restored?
- 《ClickHouse原理解析与应用实践》读书笔记(4)
猜你喜欢
随机推荐
微信小程序使用rich-text中图片宽度超出问题
Design and implementation of tcp/ip series overview
Average two numbers
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
JSON Web Token----JWT和傳統session登錄認證對比
Learning multi-level structural information for small organ segmentation
InputStream/OutputStream(文件的输入输出)
2022.7.2-----leetcode. eight hundred and seventy-one
Arc135 a (time complexity analysis)
One click filtering to select Baidu online disk files
ES6 模块化
fastjson
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
js获取对象中嵌套的属性值
Sleep quality today 78 points
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
JS how to convert seconds into hours, minutes and seconds display
每周小结(*63):关于正能量
js arguments参数使用和详解
![[untitled]](/img/32/cfd45bb5e8555ea2ad344161370dbe.png)








