当前位置:网站首页>双色球案例
双色球案例
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("]");
}
}
结果
没办法,我就是天选之人!!!
边栏推荐
- 70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
- How to clone objects
- 如何判断数组中是否含有某个元素
- Wechat applet +php realizes authorized login
- 509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
- Use of hutool Pinyin tool
- Distributed cap theory
- Webrtc quickly set up video call and video conference
- 509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
- 实用的小工具指令
猜你喜欢
复合非线性反馈控制(二)
My NVIDIA developer journey - optimizing graphics card performance
一键过滤选择百度网盘文件
卸载Google Drive 硬盘-必须退出程序才能卸载
AWT introduction
[microservice] Nacos cluster building and loading file configuration
如何展开Collapse 的所有折叠面板
接地继电器DD-1/60
Actual cases and optimization solutions of cloud native architecture
input显示当前选择的图片
随机推荐
Actual cases and optimization solutions of cloud native architecture
Design and implementation of tcp/ip series overview
C realize Snake games
win10清除快速访问-不留下痕迹
剑指 Offer II 038. 每日温度
One click filtering to select Baidu online disk files
Tutle clock improved version
How to get the parent node of all nodes in El tree
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
(4) Canal multi instance use
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
Qt发布多语言国际化翻译
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
Grounding relay dd-1/60
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
ABAP:OOALV实现增删改查功能
Distributed cap theory
Win10 clear quick access - leave no trace
Json Web token - jwt vs. Traditional session login Authentication