java - What's wrong with this code to make it not add the divisors to sum each time? -
I am trying to write a program for a laboratory in class, and I believe that I , But it will not add partners by joining the div (code in the code) and if they return the correct number and wrong otherwise the truth will compile. My code compiles and runs fine, but only a false return, Staying on (it starts after 1) Each number in the denominator). The number is brought from a private built-in constructure and is related to the set statements.
public boolean is successful () {int x = number -1; Int div = 0; Int sum = 1; Whereas (x> 1) {if (number% x == 0) {div = x; Sum = + div; } X--; } If (amount == number) {return true; } Other {return false; }}
as well as + = / = + Issue, your code also says that 1 is one, which is wrong (6 is the first complete number). The reason for this is that you start with the sum of 1, the loop is not executed exactly, then you compare sum and number That is equal to 1. You can only add a check for this particular case, for example if (sum == number and number> gt; 1) ... Additional Tip - Instead:
if (amount == number) {return true; } Other {return false; } You can use just:
returns (amount == number);
Comments
Post a Comment