Let’s talk about overcalculation! Is your calculation method overcalculated? The game is about to be officially launched, and today I found a bug that made me laugh and cry. The data calculation overflowed; the gold recharged by the player became 0; this is a big deal, after all, no one can bear this responsibility; Let me explain the reason. The development language is Java and the tool is NetBeans IDE 8.0.2 The player object has an attribute called gold which is of int type; The calculation method when a player recharges is as follows.
It seems like there is nothing wrong with it, right? Of course, the above is a simulation; If you are experienced, or can see some clues below, then it will be over-calculated; Maybe you can't see any problem. I didn't find any problem at the beginning, so I wrote the code like this. Well, let's simulate the copy code
Well, guess what the output will be? Maybe you will answer output 2. Yes, I thought it would output 2. But why is the running result 1? First, let's analyze why we thought it would output 2? Then it is obvious that we put gold + tempGold The calculation of these two values is assumed to be long type and greater than Integer.MAX_VALUE However, this is not the case. Let me take a look at the output. Copy code
In Java, the addition of gold + tempGold does not become a long but a negative number. Seeing this, maybe you will laugh at me. Well, I admit that I have not verified this problem. Fortunately, the game has not been launched yet. The problem was found in the test. Anyway, now that I have discovered the problem and understand what the problem is, I will try to solve it. That is, the problem of converting int to long
How about testing the current output?
These are correct, ok. . The stupid things are over. But I found that there is an extra variable long tempLGold; the properties and operations are not very convenient. Is there any better operation?
Note the 0L at the end Look at the output
The result is still output 1. Maybe you will laugh at me for being stupid. I am indeed stupid. After further investigation, I found out that it is a problem of operator priority. Okay, then change it.
Output
Well enough
This is correct now. These are the mistakes made by a programmer who has made mistakes... |
<<: Battery and memory have once again become Google's development focus?
>>: Featured recommendation: Detailed explanation of the use of xUtils framework
Operations personnel often use user retention ana...
The App invitation mechanism is a feature that al...
The larger the parameters of an artificial intell...
(Statement: This article is from Knowledge Keer. ...
Since college, I realized that proactively creati...
Faced with an increasing number of channels, CPs ...
Recently, the topic #Youth 3 contestant Jiang Zhi...
In a few days, the 2022 Beijing Paralympic Winter...
More than ten years have passed, and from the ini...
Recent temperature rise In weather of 30°C+ Weari...
Want to get a piece of the mobile development pie...
I guess everyone has been bidding for some time. ...
Science Times reporter epic Did you know that our...
"The more sour the fruit tastes, the higher ...
At 11 a.m. on July 8, 2022, at the Shirahama Aqua...