当前位置:网站首页>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 !!!
边栏推荐
- Sort list tool class, which can sort strings
- 复合非线性反馈控制(二)
- AWT常用组件、FileDialog文件选择框
- webrtc 快速搭建 视频通话 视频会议
- C实现贪吃蛇小游戏
- Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
- 2022.7.2-----leetcode. eight hundred and seventy-one
- Design and implementation of tcp/ip series overview
- 如何获取el-tree中所有节点的父节点
- Design and implementation of redis 7.0 multi part AOF
猜你喜欢
[March 3, 2019] MAC starts redis
How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below
如何获取el-tree中所有节点的父节点
如何展开Collapse 的所有折叠面板
JSON Web Token----JWT和传统session登录认证对比
JS扁平化数形结构的数组
js如何将秒转换成时分秒显示
Detectron: train your own data set -- convert your own data format to coco format
实用的小工具指令
QT get random color value and set label background color code
随机推荐
How to avoid JVM memory leakage?
[March 3, 2019] MAC starts redis
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
A little understanding of GSLB (global server load balance) technology
JS arguments parameter usage and explanation
How to determine whether an array contains an element
js arguments参数使用和详解
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
High performance parallel programming and optimization | lesson 02 homework at home
冲击继电器JC-7/11/DC110V
InputStream/OutputStream(文件的输入输出)
How to implement cross domain requests
Win10 clear quick access - leave no trace
Leakage detection relay jy82-2p
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
MySQL的information_schema数据库
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?
[Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school