Debug your code. As in, take out a pen, be the computer. You answer, without running this code, what it should do. Then check what it actually does with a debugger (or sysout statements if you must). There where you find a difference, you found a bug.
For example, Math.sqrt(4), what's that? is 2 less than 2?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…