当前位置:网站首页>Bicolor case
Bicolor case
2022-07-04 06:14:00 【Zhangmingming】
p69 The task is described as follows
It feels better than writing data structures c The code is a lot simpler !!
hope 10 God, get started !
package com.itheima.practice;
import java.util.Random;
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
// Generated random lucky numbers
int[] Lucknumber=creatLuckNumber();
// The selection number entered by the user
int[] Usernumber=userInputNumer();
// Judge the winning situation
judge(Lucknumber,Usernumber);
}
// How to generate lucky numbers
public static int[] creatLuckNumber(){
// Define a dynamic array , Storage 7 A digital
int[] numbers=new int[7];
// Traversal array , Generate 7 A lucky number
// The first six are red balls (1-33) Can't repeat , The last blue ball (1-16)
Random r =new Random();
for (int i = 0; i < 7; i++) {
while(true){
// Generate a 1-33 The random number
int data=r.nextInt(33)+1;
boolean flag=true;
// Check for repetition
for (int j = 0; j < i; j++) {
if(numbers[i]==data){
flag=false;
break;
}
}
if(flag){
numbers[i]=data;
break;
}
}
}
// Generate for blue ball 1-16 The random number
numbers[6]=r.nextInt(16)+1;
return numbers;
}
// User input method of number selection
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(" Please enter the number "+(i+1)+" Red number (1-33)");
int data=sc.nextInt();
numbers[i]=data;
}
System.out.println(" Please enter the blue number (1-16)");
numbers[6]=sc.nextInt();
return numbers;
}
public static void judge(int[] Lucknumber,int[] usernumber) {
int redHit=0;
int blueHit=0;
// Judge how many red balls hit
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if(Lucknumber[j]==usernumber[i]){
redHit++;
break;
}
}
}
// Judge whether the last one hits
if(Lucknumber[6]==usernumber[6]) blueHit++;
// Show winning details
System.out.print(" The lucky number is :");
printarray(Lucknumber);
System.out.println(" The number you chose is :");
printarray(usernumber);
System.out.println(" The red ball hit "+redHit+" individual , Blue ball hit "+blueHit+" individual ");
// Judge the winning situation
if(blueHit==1&&redHit<3) System.out.println(" Congratulations on your success 5 element ");
else if(blueHit==1&&redHit==3||blueHit==0&&redHit==4) System.out.println(" Congratulations on your success 10 element ");
else if(blueHit==1&&redHit==4||blueHit==0&&redHit==5) System.out.println(" Congratulations on your success 200 element ");
else if(blueHit==1&&redHit==5) System.out.println(" Congratulations on your success 3000 element ");
else if(blueHit==0&&redHit==6) System.out.println(" Congratulations on your success 500 Ten thousand yuan ");
else if(blueHit==1&&redHit==6) System.out.println(" Congratulations on your success 1000 Ten thousand yuan ");
else System.out.println(" I'm sorry I didn't win the prize ");
}
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("]");
}
}
result
Can't , I am the chosen one !!!
边栏推荐
- Webrtc quickly set up video call and video conference
- AWT介绍
- Sleep quality today 78 points
- Json Web token - jwt vs. Traditional session login Authentication
- Component、Container容器常用API详解:Frame、Panel、ScrollPane
- Excel comparator
- 如何展开Collapse 的所有折叠面板
- Fast power (template)
- js获取对象中嵌套的属性值
- Uninstall Google drive hard drive - you must exit the program to uninstall
猜你喜欢

Learning multi-level structural information for small organ segmentation

冲击继电器JC-7/11/DC110V

Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式

Design and implementation of redis 7.0 multi part AOF

509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费

Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor

A little understanding of GSLB (global server load balance) technology

JSON web token -- comparison between JWT and traditional session login authentication

Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?

Inputstream/outputstream (input and output of file)
随机推荐
el-select如何实现懒加载(带搜索功能)
Notes and notes
px em rem的区别
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Kubernets first meeting
Halcon image calibration enables subsequent image processing to become the same as the template image
4G wireless all network solar hydrological equipment power monitoring system bms110
198. House raiding
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Invalid revision: 3.18.1-g262b901-dirty
Uninstall Google drive hard drive - you must exit the program to uninstall
Nexus 6p从8.0降级6.0+root
Webrtc quickly set up video call and video conference
left_ and_ right_ Net normal version
How to realize multi account login of video platform members
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
如何实现视频平台会员多账号登录
体验碎周报第 102 期(2022.7.4)
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Component、Container容器常用API详解:Frame、Panel、ScrollPane