“This is the sixth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Record some potholes you have encountered recently and share them with you.

1. The teacher told me that the denominator can’t be 0.

Scenario: The project has a function called the guessing function, and the gold required for guessing is based on the formula:

M + N * 0.5 / diffDays. M is the value configured according to the player level plan, N is the sum of income from a certain day to the current time, and diffDays is the number of days of income.

During the test, the students found it strange that the player could not guess even with 1.4 billion gold coins. After debugging, diffDays is 0. Didn’t the teacher tell me right?

Uncomfortable.

0.0 is not 0 in memory, but an imprecise number. It may be 0.000000000001 or something else, so a positive number divided by 0.0 is very, very large. So it returns the maximum value of Long.

2, Why must break the switch?

Scenario: When writing a GM command, all gm of a function is defined in a protocol, and the operation is distinguished by case. It is assumed that {} is the end of the statement, and multiple cases are executed at the same time. The following code

Explore why? Want to know why it all starts at the beginning: bytecode

You can see that switch cases are compiled to Lookupswitch, two cases are compiled to L2,L3, and default L4 is automatically added.

Now what do you do when you join Break?

After adding break, L5 is added after L2, and GOTO L4. L4 executes 18 lines directly, that is, the end of the program, and does not execute the following case.

In addition to Lookupswitch, tablesWitch, do you know the specific differences? We’ll talk about that in the next video.

Java8 stream and Limit

Scenario: recently there was a feature to record the top 3 players in the arena, but only those from the last 5 seasons were kept, the excess was deleted. The following code is written:

The result of the run is:

Mom: 5 seasons is not what I thought it would be at all. Here’s why: Java8 has been written quite a bit, but some functions are still taken for granted. Because there are multiple season data in this set, 5 are selected at the end of the limit process, and only 8 and 9 are selected at the end. Use distinct to resolve duplicate data.

Conclusion: moving bricks is not easy, or need to pay attention to details. If you encounter any pits, you can leave a comment and share, remember to like to share.

2. Switch cannot forget break, 3. Limit filters all data.