Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RedPacket.java #119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions src/main/java/com/crossoverjie/red/RedPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ public class RedPacket {
* 最大的红包是平均值的 TIMES 倍,防止某一次分配红包较大
*/
private static final double TIMES = 2.1F;

//变量定义
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请将注释换为 Javadoc 的形式。

private static int STATUS = 1;
private int recursiveCount = 0;

public List<Integer> splitRedPacket(int money, int count) {
public List<Integer> splitRedPacket(int money, int count) throws Exception {
List<Integer> moneys = new LinkedList<>();

//金额检查,如果最大红包 * 个数 < 总金额;则需要调大最小红包 MAX_MONEY
if (MAX_MONEY * count <= money) {
System.err.println("请调大最小红包金额 MAX_MONEY=[" + MAX_MONEY + "]");
return moneys ;
//金额检查,如果最大红包 * 个数 < 总金额或最小红包 * 个数 > 总金额;则需要调整红包总金额
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里没必要抛异常了,像以前直接打印也没问题。不然所有的单测都需要改。

if (MAX_MONEY * count < money || MIN_MONEY * count > money) {
throw new Exception();
}


Expand Down Expand Up @@ -82,12 +82,21 @@ private int randomRedPacket(int totalMoney, int minMoney, int maxMoney, int coun
maxMoney = maxMoney > totalMoney ? totalMoney : maxMoney;

//在 minMoney到maxMoney 生成一个随机红包
int redPacket = (int) (Math.random() * (maxMoney - minMoney) + minMoney);
int redPacket = 0;
double temp = (Math.random() * (maxMoney - minMoney) + minMoney);
if(STATUS == LESS){
redPacket = (int) Math.floor(temp);
}else if(STATUS == MORE){
redPacket = (int) Math.ceil(temp);
}else{
redPacket = (int) temp;
}

int lastMoney = totalMoney - redPacket;

int status = checkMoney(lastMoney, count - 1);

STATUS = status;

//正常金额
if (OK == status) {
return redPacket;
Expand Down Expand Up @@ -132,14 +141,19 @@ private int checkMoney(int lastMoney, int count) {

public static void main(String[] args) {
RedPacket redPacket = new RedPacket();
List<Integer> redPackets = redPacket.splitRedPacket(20000, 100);
System.out.println(redPackets);

int sum = 0;
for (Integer red : redPackets) {
sum += red;
}
System.out.println(sum);
List<Integer> redPackets = null;
try {
redPackets = redPacket.splitRedPacket(20000, 100);
System.out.println(redPackets);

int sum = 0;
for (Integer red : redPackets) {
sum += red;
}
System.out.println(sum);
} catch (Exception e) {
System.err.println("请调整红包总金额 !");
}
}

}