Answer the question
In order to leave comments, you need to log in
Why doesn't the function return a boolean variable?
Good afternoon, tell me please. I recently started learning Java and wrote a function that doesn't work, why? rewrote the function to javascript, everything works correctly.
public static boolean simpleNumber(int number){
boolean result;
for(int i = 1; i < number; i++){
if(number % i == 0) {
result = true;
}
}
return result;
}
Answer the question
In order to leave comments, you need to log in
The "variable result might not have been initialized"
error means that when a variable is returned from a function, it may not have been defined yet. That is, if result = true does not happen , then the variable will not have a value (because at the beginning you simply created it - boolean result , but did not assign any value), the compiler swears at this. In your case, you just need to do:
Then the variable will definitely have some value at the time of return.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question