当前位置:网站首页>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 !!!
边栏推荐
- How to solve the component conflicts caused by scrollbars in GridView
- Json Web token - jwt vs. Traditional session login Authentication
- Arc135 C (the proof is not very clear)
- JS execution mechanism
- win10清除快速访问-不留下痕迹
- Upper computer software development - log information is stored in the database based on log4net
- Configure cross compilation tool chain and environment variables
- [microservice] Nacos cluster building and loading file configuration
- 如何判断数组中是否含有某个元素
- Sort list tool class, which can sort strings
猜你喜欢

Sleep quality today 78 points

How to solve the component conflicts caused by scrollbars in GridView

Detectron: train your own data set -- convert your own data format to coco format

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

ES6 modularization

How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below

Win10 clear quick access - leave no trace

C语言练习题(递归)

C language - Blue Bridge Cup - Snake filling

How much computing power does transformer have
随机推荐
Distributed cap theory
安装 Pytorch geometric
JSON Web Token----JWT和传统session登录认证对比
The difference between PX EM rem
left_ and_ right_ Net normal version
如何获取el-tree中所有节点的父节点
How to solve the component conflicts caused by scrollbars in GridView
JSON web token -- comparison between JWT and traditional session login authentication
How to get the parent node of all nodes in El tree
How to expand all collapse panels
Tutle clock improved version
Online shrimp music will be closed in January next year. Netizens call No
Weekly summary (*63): about positive energy
buuctf-pwn write-ups (8)
双色球案例
Abap:ooalv realizes the function of adding, deleting, modifying and checking
Arc135 a (time complexity analysis)
手动对list进行分页(参数list ,当前页,页面大小)
MySQL information_ Schema database
px em rem的区别