2011-11-05 69 views
-2

我有一個方法返回到主要方法的問題。這就是說,「回報金額」中的金額不能解析爲變量。我在哪裏關閉?返回問題的方法

這是我得到的消息:在該行 多個標記 - 太虛方法不能返回 值 - 量不能被解析爲 可變

下面是代碼:

import java.util.Scanner; 

public class Investment { 
    public static void main(String[]args) { 
     Scanner input = new Scanner(System.in); 

     System.out.print("Enter the amount invested: "); 
     double amount = input.nextDouble(); 

     System.out.print("Enter the annual interest rate: "); 
     double interest = input.nextDouble(); 

     int years = 30; 

     System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table 
    } 

    public static double futureInvestmentValue(double amount, double interest, int years) { 

     double monthlyInterest = interest/1200; 
     double temp; 
     double count = 1; 

     while (count < years) 
      temp = amount * (Math.pow(1 + monthlyInterest,years *12)); 
     amount = temp; 

     System.out.print((count + 1) + " " + temp); 
    } 

    { 
     return amount; 
    } 
} 
+4

嘗試縮進代碼。使閱讀更容易。 –

+1

你不能從main方法返回一些東西。刪除返回並打印一些消息或錯誤。 – sathis

+0

請添加適當的標籤並縮進您的代碼。 – svinto

回答

0

你的花括號不正確。編譯器和我對此感到困惑。

這應該工作(至少語法):

import java.util.Scanner; 

public class Investment { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     System.out.print("Enter the amount invested: "); 
     double amount = input.nextDouble(); 

     System.out.print("Enter the annual interest rate: "); 
     double interest = input.nextDouble(); 

     int years = 30; 

     System.out.print(futureInvestmentValue(amount, interest, years)); 
    } 

    public static double futureInvestmentValue(
      double amount, double interest, int years) { 

     double monthlyInterest = interest/1200; 
     double temp = 0; 
     double count = 1; 

     while (count < years) 
      temp = amount * (Math.pow(1 + monthlyInterest, years * 12)); 
     amount = temp; 

     System.out.print((count + 1) + " " + temp); 

     return amount; 
    } 
} 
+0

它不必在該塊中聲明;外部範圍是可見的。 –

+0

@DaveNewton:你說的對,但是我的內部Java編譯器很困惑...... – jeha

+0

不,我認爲你是對的;我誤解了原始代碼 - 它根本不在方法內部。 –

0

從它自己的範圍作爲一個開始刪除amount。同樣從方法futureInvestmentValue中,您將amount作爲參數,但該值永遠不會被修改,因此您將返回傳遞的相同值,這很可能不是所需的結果。

0
  1. 從它自己的範圍內刪除return amount
  2. 方法futureInvestmentValue ...你不能修改任何參數的方法裏面,所以你必須申報金額之外的另一個變量的方法內(也許這是temp變量,你繼續使用),並返回,而不是
  3. 當你返回的東西,返回語句總是在方法內。決不外面,而裏面自己的括號(從來沒見過這個...)

    import java.util.Scanner; 
    
    public class Investment { 
        public static void main(String[]args) { 
         Scanner input = new Scanner(System.in); 
    
         System.out.print("Enter the amount invested: "); 
         double amount = input.nextDouble(); 
    
         System.out.print("Enter the annual interest rate: "); 
         double interest = input.nextDouble(); 
    
         int years = 30; 
    
         System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table 
        } 
    
        public static double futureInvestmentValue(double amount, double interest, int years) { 
    
         double monthlyInterest = interest/1200; 
         double temp; 
         double count = 1; 
    
         while (count < years) { 
          temp = amount * (Math.pow(1 + monthlyInterest,years *12)); 
          System.out.print((count + 1) + " " + temp); 
         } 
    
         return amount; 
        } 
    }