I am working in java and came across an odd block of code that my fellow college wrote i'm sure the modulus operator would work but for some reason i'm not getting the expected results i thought i would from the operator.
right now the code is written as follows:
long divisionID = myMaxId % 40;
if (divisionID == 0) {
divisionID = 1;
}
long empiresubID = 1;
if (myMaxId >= 1600) {
empiresubID = 2;
}
if (myMaxId >= (1600 * 2)) {
empiresubID = 3;
}
if (myMaxId >= (1600 * 3)) {
empiresubID = 4;
}
if (myMaxId >= (1600 * 4)) {
empiresubID = 5;
}
if (myMaxId >= (1600 * 5)) {
empiresubID = 6;
}
it's like this for a few other places and this goes up through 1600*40.
i thought using the modulus operator for myMaxId %1600 but that gave me way incorrect results.
the end goal is there are 40 divisions, we first fill sub division 1 for all divisions first using myMaxID % 40 as users register.
then once that is done the subdivision would then flip to 2, then we fill in all the divisions subdivision 2.
i don't think the way it's currently programmed is efficient and there should be some other way to do this.
any thoughts or help on this would be great.
I think you can get rid of the chain of ifs by replacing with
long empiresubID = (myMaxId / 1600) + 1;
Your method can be replaced by two lines:
long divisionID = myMaxId % 40 == 0 ? 1 : myMaxId % 40;
long empiresubID = (myMaxId / 1600) + 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With