+ 8
I have a question about recursion
I created a method in java called check that uses recursion to calculate something and return boolean. My code didn't work when I just wrote check(); in to method to make recursion, but it did work when I wrote: return check();. Can someone explane me whats the difference? Thank you:)
14 odpowiedzi
+ 6
😂
+ 5
Yes but its not where the recursion stops and the method returns a value. It is just where I need the method to call itself. Threre is another place where the method returns true or false after the calculation finished
+ 5
I think you mean about the return symm(a); and the if(symm(a1)).
in this case, try to understand the if :
if(1234) means nothing. but
if(true) or if(false) return a condition.
+ 5
The method should check if the array is symmetric.
For example the array {2, 4, 7, 8, 11, 13} is symmetric because 2+13 = 15, 4+11 = 15 and 7+8 = 15.
+ 5
oh ok, but my languages are web languages. now it's okay. let me time
+ 5
your prog works normally, why need you help ?
+ 5
I got an exercise and it says that I must use recursion
+ 4
in a time you just execute your function, in a second time you say to output result of your function
+ 4
public class SYMM {
static int i = 0;
static int sum;
public static boolean symm(int[] a) {
if(i == 0) {
sum = a[0] + a[a.length-1];
}
if(sum == a[i] + a[a.length-i-1]) {
if(i+1 == a.length/2) {
return true;
}
i++;
return symm(a);
}
return false;
}
public static void main(String[] args) {
int[] a1 = {2, 3, 4, 6, 7, 8};
if(symm(a1)) {
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
+ 4
Because I still didn't understand why is it working when I write "return symm(a);" instead of just calling it simply "symm(a);"
+ 4
oh I understand. why dont use you a for loop ?
+ 3
can I have the code please ?
+ 3
symm(a) execute the function and
return symm(a) return the result of the function (true or false)
+ 2
This is recursion
https://www.sololearn.com/Discuss/1229035/?ref=app