Reputation: 9
I am trying to do the following program in Java
where I'm writing a recursive and an iterative method to compute the sum of all odd numbers from n
to m
import java.util.Scanner;
public class AssignmentQ7 {
public static int recursivesum(int n, int m){ if (n < m){ int s = n; s += recursivesum(n+2, m); } else{ if(m < n){ int s = m; s += recursivesum(m+2, n); } } return s; } public static int iterativesum(int n, int m){ if(n < m){ int s = n; for(int i = n; i <= m; i += 2){ s += i; return s; } } else if(m < n){ int s = m; for(int i = m; i <= n; i += 2){ s += i; return s; } } } public static void main(String args[]){ int n,m; Scanner in = new Scanner(System.in); System.out.println("Enter two numbers: "); n = in.nextInt(); m = in.nextInt(); while(n%2 == 0){ System.out.println("Enter the first number again: "); n = in.nextInt(); } while(m%2 == 0){ System.out.println("Enter the second number again: "); m = in.nextInt(); } System.out.println("The answer of the recursive sum is: " + recursivesum(n,m)); System.out.println("The answer of the iterative sum is: " + iterativesum(n,m)); } }
I'm getting an error cannot find symbol - variable enter code here
s. I don't know what's wrong! Can anyone help please?
Upvotes: 0
Views: 969
Reputation: 1
Your code must be like this, you dont have to use two for loop
.
import java.util.Scanner;
public class AssignmentQ7 {
public static int recursivesum(final int n, final int m) {
final int lower = n < m ? n : m;
final int upper = n > m ? n : m;
final int total = lower;
if (lower >= upper) {
return total;
}
return total + AssignmentQ7.recursivesum(lower + 2, upper);
}
public static int iterativesum(final int n, final int m) {
final int lower = n < m ? n : m;
final int upper = n > m ? n : m;
int total = 0;
for (int num = lower; num <= upper; num = num + 2) {
total += num;
}
return total;
}
public static void main(final String args[]) {
int n, m;
final Scanner in = new Scanner(System.in);
System.out.println("Enter two numbers: ");
n = in.nextInt();
m = in.nextInt();
while (n % 2 == 0) {
System.out.println("Enter the first number again: ");
n = in.nextInt();
}
while (m % 2 == 0) {
System.out.println("Enter the second number again: ");
m = in.nextInt();
}
System.out.println("The answer of the recursive sum is: " + AssignmentQ7.recursivesum(n, m));
System.out.println("The answer of the iterative sum is: " + AssignmentQ7.iterativesum(n, m));
}
}
Upvotes: 0
Reputation: 101
You have an error in the first method where you define s
outside the scope which you return it from. In the second method you return inside the loop.
As others in this thread suggests, use an IDE like Eclipse (https://www.eclipse.org/) or IntelliJ (http://www.jetbrains.com/idea/)
import java.util.Scanner;
public class AssignmentQ7 {
public static int recursivesum(int n, int m) {
int s = n;
if (n < m) {
s += recursivesum(n + 2, m);
}
else {
if (m < n) {
s = m;
s += recursivesum(m + 2, n);
}
}
return s;
}
public static int iterativesum(int n, int m) {
int s = 0;
if (n < m) {
for (int i = n; i <= m; i += 2) {
s += i;
}
}
else if (m < n) {
for (int i = m; i <= n; i += 2) {
s += i;
}
}
return s;
}
public static void main(String args[]) {
int n, m;
Scanner in = new Scanner(System.in);
System.out.println("Enter two numbers: ");
n = in.nextInt();
m = in.nextInt();
while (n % 2 == 0) {
System.out.println("Enter the first number again: ");
n = in.nextInt();
}
while (m % 2 == 0) {
System.out.println("Enter the second number again: ");
m = in.nextInt();
}
System.out.println("The answer of the recursive sum is: " + recursivesum(n, m));
System.out.println("The answer of the iterative sum is: " + iterativesum(n, m));
}
}
Upvotes: 0
Reputation: 1421
It's scope problem. You're declaring variable s inside the if statements which is (local definition) of variable.
You need to modify the two methods as follows:
The recursive method will be
public static int recursivesum(int n, int m) {
int s = 0;
if (n < m) {
s = n;
s += recursivesum(n + 2, m);
} else {
if(m < n){
s = m;
s += recursivesum(m + 2, n);
}
}
return s;
}
And the iterative method will be:
public static int iterativesum(int n, int m) {
int s = 0;
if(n < m) {
s = n;
for(int i = n; i <= m; i += 2) {
s += i;
}
} else {
if(m < n) {
s = m;
for(int i = m; i <= n; i += 2) {
s += i;
}
}
}
return s;
}
Upvotes: 0
Reputation: 1500675
This method is the problem:
public static int recursivesum(int n, int m) {
if (n < m) {
int s = n; // Variable declared here... scoped to the if
s += recursivesum(n+2, m);
} else {
if (m < n) {
int s = m; // Variable declared here... scoped to this if
s += recursivesum(m+2, n);
}
}
return s; // No such variable in scope!
}
You could use:
public static int recursivesum(int n, int m) {
int s = 0; // See note below
if (n < m) {
s = n + recursivesum(n+2, m);
} else {
if (m < n) {
s = m + recursivesum(m+2, n);
}
}
return s;
}
We have to give s
an explicit initial value, because you currently don't have any code handling the case where n
and m
are equal. It's not clear what you want to do then.
Another alternative is to return from the if
statements, just as you do in iterativesum
... although you'll again need to think about what to do if m == n
:
public static int recursivesum(int n, int m) {
if (n < m) {
// You don't even need an s variable!
return n + recursivesum(n+2, m);
} else if (m < n) {
// Simplified else { if { ... } } to else if { .... }
return m + recursivesum(m+2, n);
} else {
// What do you want to return here?
}
}
Note that you've got the same problem in iterativesum
- the compiler should be complaining at you at the moment that not all paths return a value. What do you expect iterativesum(3, 3)
to do?
Upvotes: 5
Reputation: 11234
You are declaring variable s
inside if
statement, that is why you get such error. Start from declaration int s
outside if
statement.
Upvotes: 0
Reputation: 1473
Try this:
int s;
if (n < m){
s = n;
s += recursivesum(n+2, m);
} else{
if(m < n){
int s = m;
s += recursivesum(m+2, n);
}
}
return s;
Upvotes: 0
Reputation: 1195
In the recursivesum(int n,int m)
function the scope of variable s
is inside the if and else
block. When your returning s
it is out of scope.
Try using some IDE's like eclipse
. So that you can debug these errors instantly
Upvotes: 0
Reputation: 32468
in recursivesum(int n, int m)
method, you have declared s
within if condition, but, you tried to access it in else part.
public static int recursivesum(int n, int m){
int s = n; // Now s having method local scope
if (n < m){
s += recursivesum(n+2, m);
} else{
if(m < n){
int s = m;
s += recursivesum(m+2, n);
}
}
return s;
}
Upvotes: 2