2016-04-03 57 views
-3

在下面的代碼中,我不斷收到一個錯誤,說明該類型必須是一個數組,但是它在print語句中返回一個double [「total [agentnumber]」)。任何人都可以解釋這一點,我可以在這種情況下解決它嗎?如何返回Java中數組的元素作爲double?

我的目標是:在代碼輸入週期數,利率和代理人數量(每個代理人被分配一個等於其代理人數量的委託人,即代理人1收到100,代理人收到200等等) )並打印每個代理商的餘額。我想在循環中執行此操作,而不是使用複合興趣公式,因爲我正在嘗試學習如何使用Java。

其他問題:這是有點瘋狂的設置,因爲我不知道如何返回for循環的結果。我應該在哪裏放置「退貨」,以及我應該返還什麼,假設我有興趣最終爲每個代理打印代理[代理號碼]。貨幣的等價物。

public class Person{ 

    public double money; // initialize 

    Person(double cash){ 
     money = cash; 
    } 
} 
public class Bank { 

    public static double bank(double rate, double periods, int agents){ 
     Person[] agent = new Person[agents]; 
     double total; 

     for(int agentnumber = 0; agentnumber < agents; agentnumber++) { 
      agent[agentnumber] = new Person((agentnumber+1)*100); // assign starting incomes and instantiate players 

      for(int months = 0; months < periods; months++){ 
       agent[agentnumber].money = agent[agentnumber].money*(1.0 + rate); 
       total = agent[agentnumber].money; 
       System.out.println("The total balance of player " + agentnumber + " is " + total[agentnumber]); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     bank(0.05,120,3); 
    } 
} 
+1

已經添加了當詢問錯誤,發佈完整和準確的錯誤信息演示我會改變。 –

+0

看看你如何使用'total'。查看錯誤消息指示的代碼行,看看是否一切都合理。仔細查看錯誤消息。 –

+3

我不認爲這會編譯出於多種原因。 –

回答

0

total是雙,所以只是打印total出來,而不是total[agentnumber]

0

我一直在下面的代碼時收到錯誤告訴我,類型必須是一個數組,但它在打印聲明中返回了「total [agentnumber]」的兩倍。任何人都可以解釋這一點,我可以在這種情況下解決它嗎?

您會收到此錯誤消息,因爲total是雙精度數組,而不是數組。爲了使用[],該類型必須是數組。解決方法是打印total而不是total[agentnumber]

我應該在哪裏放置「退貨」以及我應該返回什麼,假設我有興趣最終爲每個代理打印代理[代理號碼]。貨幣的等價物。

當你的程序是正確的,現在寫的,你會得到一個錯誤,因爲你已經宣佈bank()將返回了一倍,但你有沒有return聲明bank()。目前還不清楚爲什麼你想從bank()返回任何東西。你可能想改變

public static double bank(double rate, double periods, int agents) 

public static void bank(double rate, double periods, int agents) 

,然後你不需要任何回報。

0

什麼和我應該在哪裏返回?

您似乎只是想打印出數組的值,所以沒有必要返回。在這種情況下,您可以使該方法具有void返回類型。這也不是很清楚爲什麼你需要陣列,因爲main方法不需要本地agents變量,所以如果你確實需要陣列,你應該返回Person[]而不是double ...

你也可以像這樣清理代碼。

public class Bank { 

    public static Person[] bank(double rate, double periods, int agents){ 
     Person[] agent = new Person[agents]; 

     for(int agentnumber = 0; agentnumber < agents; agentnumber++) { 
      Person p = new Person((agentnumber+1)*100); // assign starting incomes and instantiate players 

      for(int months = 0; months < periods; months++){ 
       p.money = p.money*(1.0 + rate); 
       System.out.printf("The total balance of player %d at month %d is %.2f\n", agentnumber, months, p.money); 
      } 

      agent[agentnumber] = p; 
     } 

     return agent; 
    } 

    public static void main(String[] args) { 
     Person[] agents = bank(0.05,120,3); 
    } 
} 
0

我已經去重寫了上面的代碼,並確保它編譯。有一些最佳實踐的事情,如果這不只是我的意見

public class Bank { 

    public static void main(String[] args) { 
     Bank bank = new Bank(0.05,120,3); 
    } 

    // For readability and simplification I would make this public and 
    // move it into it's own file called Person.java 
    private class Person { 
     public double money; 

     Person(double cash){ 
      money = cash; 
     } 
    } 

    public Bank(double rate, double periods, int numberOfAgents) { 
     Person[] agent = new Person[numberOfAgents]; 

     // Typical java convention is to use camel case so 
     // agentnumber would become agentNumber 
     for(int agentnumber = 0; agentnumber < numberOfAgents; agentnumber++) { 
      agent[agentnumber] = new Person((agentnumber+1)*100); 

      for(int months = 0; months < periods; months++){ 
       agent[agentnumber].money = agent[agentnumber].money*(1.0 + rate); 
      } 
      System.out.println("The total balance of player " + agentnumber + " is " + agent [agentnumber].money); 
     } 
    } 
} 
+0

對不起,忽略以上內容並閱讀此:與上述問題: (1)我試圖在120次迭代後獲得代理[代理號碼]。貨幣的罰款號碼。然而,看起來這是通過增加TO來重複計算。即代理[agentnumber] .money的最後一個值應該是我想要的,不是? (2)我正在嘗試打印代理[agentnumber] .money的最終值。我曾經想過,使用'total'會讓我做到這一點,但它會展現出上面的行爲。刪除Total = total +行並替換System.out.print語句中的總數會引發錯誤,但是... – user3614648

+0

如果我正確理解您的請求,則答案代碼現在應該按照您的要求做。我已經將整體移出循環,並直接打印更新的貨幣屬性。 – Andy