当前位置:网站首页>双色球案例
双色球案例
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("]");
}
}
结果
没办法,我就是天选之人!!!
边栏推荐
- 2022.7.2-----leetcode. eight hundred and seventy-one
- C语言中的函数(详解)
- js arguments参数使用和详解
- Understanding of cross domain and how to solve cross domain problems
- 如何展开Collapse 的所有折叠面板
- JSON web token -- comparison between JWT and traditional session login authentication
- Abap:ooalv realizes the function of adding, deleting, modifying and checking
- Arc135 a (time complexity analysis)
- C realize Snake games
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
猜你喜欢
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
实用的小工具指令
卸载Google Drive 硬盘-必须退出程序才能卸载
AWT introduction
如何避免 JVM 内存泄漏?
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
Halcon image calibration enables subsequent image processing to become the same as the template image
webrtc 快速搭建 视频通话 视频会议
Take you to quickly learn how to use qsort and simulate qsort
JS arguments parameter usage and explanation
随机推荐
Excel comparator
接地继电器DD-1/60
How to get the parent node of all nodes in El tree
4G wireless all network solar hydrological equipment power monitoring system bms110
注释与注解
C language - Blue Bridge Cup - Snake filling
剑指 Offer II 038. 每日温度
js如何将秒转换成时分秒显示
How to solve the component conflicts caused by scrollbars in GridView
报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
Fast power (template)
JSON web token -- comparison between JWT and traditional session login authentication
AWT introduction
《ClickHouse原理解析与应用实践》读书笔记(4)
(4) Canal multi instance use
A little understanding of GSLB (global server load balance) technology
The difference between PX EM rem
Json Web token - jwt vs. Traditional session login Authentication
Actual cases and optimization solutions of cloud native architecture
Configure cross compilation tool chain and environment variables