当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢
随机推荐
在大规模 Kubernetes 集群上实现高 SLO 的方法
直播预告 | 微服务架构学习系列直播第三期
drf JWT認證模組與自定製
如何对Pandas DataFrame进行自定义排序
谁说Cat不能做链路跟踪的,给我站出来
Menu permission control configuration of hub plug-in for azure Devops extension
Group count - word length
Jmeter——ForEach Controller&Loop Controller
大数据应用的重要性体现在方方面面
Cos start source code and creator
自然语言处理之命名实体识别-tanfordcorenlp-NER(一)
中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
Dapr實現分散式有狀態服務的細節
(2)ASP.NET Core3.1 Ocelot路由
向北京集结!OpenI/O 2020启智开发者大会进入倒计时
连肝三个通宵,JVM77道高频面试题详细分析,就这?
制造和新的自动化技术是什么?
TRON智能钱包PHP开发包【零TRX归集】
遞迴思想的巧妙理解
连肝三个通宵,JVM77道高频面试题详细分析,就这?