我試圖做下面的程序在Java
,我正在寫一個遞歸計算從n
所有的奇數的總和m
找不到符號 - 可變的Java
進口的Java迭代方法.util.Scanner;
公共類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)); } }
我得到一個錯誤,無法找到符號 - 可變enter code here
秒。我不知道什麼是錯的!任何人都可以幫忙嗎?
範圍是在括號外...它定義爲類變量 – VinayVeluri
@VinayVeluri:沒有必要爲它是一個類變量。 –
在你的條件之外聲明你的變量 – David