当前位置:网站首页>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 !!!
边栏推荐
- 740. Delete and get points
- Invalid revision: 3.18.1-g262b901-dirty
- Lightroom import picture gray / Black rectangular multi display
- C實現貪吃蛇小遊戲
- Leetcode question brushing record | 206_ Reverse linked list
- Fast power (template)
- How much computing power does transformer have
- How to implement lazy loading in El select (with search function)
- AWT常用组件、FileDialog文件选择框
- MySQL的information_schema数据库
猜你喜欢
Component、Container容器常用API详解:Frame、Panel、ScrollPane
我的NVIDIA开发者之旅——优化显卡性能
How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below
C realize Snake games
QT 获取随机颜色值设置label背景色 代码
双色球案例
740. Delete and get points
Practical gadget instructions
Arc135 C (the proof is not very clear)
HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)
随机推荐
JSON Web Token----JWT和傳統session登錄認證對比
微信小程序使用rich-text中图片宽度超出问题
2022.7.2-----leetcode. eight hundred and seventy-one
A little understanding of GSLB (global server load balance) technology
How to implement cross domain requests
卸载Google Drive 硬盘-必须退出程序才能卸载
webrtc 快速搭建 视频通话 视频会议
How much computing power does transformer have
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
fastjson
JSON Web Token----JWT和传统session登录认证对比
js获取对象中嵌套的属性值
Sleep quality today 78 points
4G wireless all network solar hydrological equipment power monitoring system bms110
【微服务】Nacos集群搭建以及加载文件配置
实用的小工具指令
每周小结(*63):关于正能量
检漏继电器JY82-2P
冲击继电器JC-7/11/DC110V
Kubernets first meeting