当前位置:网站首页>Keyboard entry lottery random draw
Keyboard entry lottery random draw
2020-11-06 01:16:00 【mb5e9a38ab3ad0f】
Lottery software
a、 Enter the name of the lucky draw through the keyboard , For multiple Raffles “|” Division of no. . When you're done typing ,
The console prints the name of the raffle , And prompt YES OR NO To confirm . Once confirmed
recognize , Automatically assign to the raffle ID, Start raffle .( If there is a duplicate name , with ID Subject to )
b、 Prizes are stored in a collection .
c、1 Prize 1 individual 2 Prize 3 individual 3 Prize 4 individual .
d、 Print your name every time you draw , And then type in next Start a second draw . If the prize is already
Finish smoking , Then publish the winning summary .
No more reminders next
1. Initialize the name of the raffle
2. Initialize the prize information
Adopt the idea of object oriented programming Split code --》 Put the above two functions into the tool class
/**
* Define a winner
*/
public class LuckDog {
private Integer id;// The winner id
private String name;// The name of the winner
public LuckDog(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "LuckDog{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public LuckDog() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
**
* Define a class Prizes
*/
public class Prize {
private String name;// The name of the prize
private String level;////
private Integer count;// The number of prizes
@Override
public String toString() {
return "Prize{" +
"name='" + name + '\'' +
", level='" + level + '\'' +
", count=" + count +
'}';
}
public Prize(String name, String level, Integer count) {
this.name = name;
this.level = level;
this.count = count;
}
public Prize() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
/**
* Define a winning result
*/
public class Result {
private Integer id;//id
private String username;// The name of the winner
private String prizeName;// The winner's prize
@Override
public String toString() {
return "Result{" +
"id=" + id +
", username='" + username + '\'' +
", prizeName='" + prizeName + '\'' +
'}';
}
public Result() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPrizeName() {
return prizeName;
}
public void setPrizeName(String prizeName) {
this.prizeName = prizeName;
}
public Result(Integer id, String username, String prizeName) {
this.id = id;
this.username = username;
this.prizeName = prizeName;
}
}
/**
* Define a tool class
* Accomplish two functions
* 1. Initialize the lottery information
* 2. Initialize the prize information
*/
public class ToolsClass {
// You need containers Initialize the initial capacity of the raffle
static ArrayList<LuckDog> list = new ArrayList<>();
// Define a static method Initialize the lottery information
public static ArrayList<LuckDog> initperson() {
// Get... From the console
System.out.println(" Please take part in the raffle information , If there are more than one , Intermediate use | separate ");
Scanner sc = new Scanner(System.in);
String strs = sc.nextLine();// Lottery information
//split(String patter) Split a string into an array of characters
// Null check
if (strs != null && !strs.equals("") && !strs.equals("null")) {
// indicate Data is secure
String[] persons = strs.split("\\|");// \\ Split |
// Loop traversal persons Determine the length
for (int i = 0; i < persons.length; i++) {
// Go to list Add lottery data to the container
list.add(new LuckDog((i + 1), persons[i]));//LuckDog Storage type object
}
} else {
// If the information in front is illegal recursive
initperson();
}
// Traverse list Containers
for (int i = 0; i < list.size(); i++) {
System.out.print(" "+ list.get(i).getName()+" ");
}
// Return the results to the program
return list;
}
//2. Initialize prize information
public static ArrayList<Prize> initPrize(){
// Construct an empty prize container
ArrayList<Prize> list =new ArrayList<>();
// Store prizes Prize Class object
list.add(new Prize("IPhone12"," The first prize ",1));
list.add(new Prize(" Huawei P40"," The second prize ",3));
list.add(new Prize(" Water glass "," The third prize ",4));
return list;
}
}
/*
test
*/
public static void main(String[] args) {
// Call directly ToolClass Class initperson() Method
ArrayList<LuckDog> ldalist = ToolsClass.initperson();
// for (int i = 0; i < initperson.size(); i++) {
// System.out.println(initperson.get(i));
// }
// Call directly ToolClass Class initPrize() Method
ArrayList<Prize> prize = ToolsClass.initPrize();
// for (int i = 0; i < prize.size(); i++) {
// System.out.println(prizes.get(i));
// }
// Building a scanner object
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter 《YES NO》");
String str = sc.nextLine();
// Ignore case
if (("NO").equalsIgnoreCase(str)) {
//
System.out.println(" The lottery failed !");
return;// Program end
} else if (!("YES").equalsIgnoreCase(str)) {
System.out.println(" The input data does not meet the requirements !");
return;// Program end
} else {
// The program begins to draw
// Build the result container first
ArrayList<Result> results = new ArrayList<>();
// loop Number of cycles The total number of prizes is 8
while (ldalist.size() > 0 && prize.size() > 0) {// Conditions at the end of the cycle
// Start raffle
int index = (int) (Math.random() * ldalist.size());
// According to the index, get the corresponding raffle
LuckDog luckDog = ldalist.get(index);// Lucky draw
// Prize Smoke backwards The third prize
Prize prize1 = prize.get(prize.size() - 1);
// Splicing the winning result
results.add(new Result(luckDog.getId(), luckDog.getName(), prize1.getName()));
// The number of prizes minus one If the number of prizes is 1 The prize should be deleted
if (prize1.getCount() == 1) {
// Delete the corresponding prize
prize.remove(prize1);
} else {
// The number of prizes minus one
prize1.setCount(prize1.getCount() - 1);
}
// Delete the information of the winner of the last round
ldalist.remove(index);// Delete... According to index , Delete objects directly
// Output the winner's information
System.out.println(" The winner this time is " + luckDog.getName() + ", The winner's id yes " + luckDog.getId() + ", The prize is " + prize1.getName() + ", Winning grade " + prize1.getLevel());
// Judge again The number of people and the number of prizes
if (prize.size() > 0 && ldalist.size() > 0) {
System.out.println(" Please enter next Start the next round of lottery :");
String next = sc.next();
while (!"next".equalsIgnoreCase(next)) {
System.out.println(" Please enter next Start the next round of lottery !");
// Repeat the input next Is it next The content of
next = sc.next();
}
}
}
// The draw is over
System.out.println("---------------------------");
// Show the raffle summary
System.out.println(" Winning information results in :");
for (int i = 0; i < results.size(); i++) {
System.out.println("ID:"+results.get(i).getId() + "\t\t full name :" + results.get(i).getUsername() + "\t\t Prize name " + results.get(i).getPrizeName());
}
}
}
版权声明
本文为[This is LSP]所创,转载请带上原文链接,感谢
边栏推荐
- 加速「全民直播」洪流,如何攻克延时、卡顿、高并发难题?
- Basic principle and application of iptables
- (1) ASP.NET Introduction to core3.1 Ocelot
- 100元扫货阿里云是怎样的体验?
- Elasticsearch database | elasticsearch-7.5.0 application construction
- 速看!互联网、电商离线大数据分析最佳实践!(附网盘链接)
- How to select the evaluation index of classification model
- Filecoin最新动态 完成重大升级 已实现四大项目进展!
- Vue 3 responsive Foundation
- 【效能優化】納尼?記憶體又溢位了?!是時候總結一波了!!
猜你喜欢
选择站群服务器的有哪些标准呢?
一时技痒,撸了个动态线程池,源码放Github了
Want to do read-write separation, give you some small experience
PLC模拟量输入和数字量输入是什么
哇,ElasticSearch多字段权重排序居然可以这么玩
Using Es5 to realize the class of ES6
加速「全民直播」洪流,如何攻克延时、卡顿、高并发难题?
This article will introduce you to jest unit test
采购供应商系统是什么?采购供应商管理平台解决方案
TRON智能钱包PHP开发包【零TRX归集】
随机推荐
Use of vuepress
2018个人年度工作总结与2019工作计划(互联网)
如何在Windows Server 2012及更高版本中將域控制器降級
Microservices: how to solve the problem of link tracing
drf JWT認證模組與自定製
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
Analysis of ThreadLocal principle
在大规模 Kubernetes 集群上实现高 SLO 的方法
【新閣教育】窮學上位機系列——搭建STEP7模擬環境
Programmer introspection checklist
Basic principle and application of iptables
Jmeter——ForEach Controller&Loop Controller
[performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
3分钟读懂Wi-Fi 6于Wi-Fi 5的优势
分布式ID生成服务,真的有必要搞一个
Calculation script for time series data
快快使用ModelArts,零基礎小白也能玩轉AI!
Azure Data Factory(三)整合 Azure Devops 實現CI/CD
TRON智能钱包PHP开发包【零TRX归集】
读取、创建和运行多个文件的3个Python技巧