2013-09-29 45 views
0

我有兩個數組,都是int類型。 costs[]數組完美工作,沒有錯誤或錯誤,但stands數組編碼完全相同,在我定義的方法中出現錯誤「表達式的類型必須是數組類型,但它解析爲int」該程序的底部名爲buttonFunc。在init()方法中,我打電話stands[1] = 0,這工作正常,但是當我做幾乎相同的事情(用您在調用方法時定義的整數替換1)時,它會給我帶來錯誤。爲什麼兩個數組在語法和用法上完全相同可以做到這一點?表達式的類型必須是數組類型,但它解析爲int - 與數組的作用相同的語法?

`import java.applet.Applet; 
import java.awt.BorderLayout; 
import java.awt.Button; 
import java.awt.Canvas; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Rectangle; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferStrategy; 
import java.text.DecimalFormat; 

import javax.swing.Timer; 


public class Main extends Applet implements Runnable, ActionListener { 
    private static final long serialVersionUID = 1L; 

    double money = 0; 
    double income = 2; //Actual income is this * 0.4 
    //int lemonadeStands = 0; 
    int cookieStands = 0; 
    int cupcakeStands = 0; 
    int cookieCost = 25; 
    int cupcakeCost = 75; 
    int modifier = 1; 

    int[] costs = new int[3]; 
    int[] stands = new int[3];    //Declaring the array - same as the costs[] array, but it doesn't work? 

    Button buyLemonade; 
    Button buyCookie; 
    Button buyCupcake; 

    int time = 0; 
    int timeComparison = (int) (Math.random()*50 + 120); 

    private Graphics dBufferedGraphic = null; 
    private Image dbufferedImage = null; 

    public void init() { 
     costs[1] = 10; 
     stands[1] = 0;     //No error here? 

     setLayout(new FlowLayout()); 
     buyLemonade = new Button("Buy a Lemonade Stand"); 
     buyCookie = new Button("Buy a Cookie Stand"); 
     buyCupcake = new Button("Buy a Cupcake Stand"); 

     add(buyLemonade); 
     add(buyCookie); 
     add(buyCupcake); 

     buyLemonade.addActionListener(this); 
     buyCookie.addActionListener(this); 
     buyCupcake.addActionListener(this); 

     t.start(); 
    } 

    public void paint(Graphics g) { 
     DecimalFormat df = new DecimalFormat("#.##"); 
     g.drawString("Money: $" + df.format(money), 10, 10); 
     g.drawString("Income: $" + income/2.5 + " per second", 10, 24); 
     g.drawString("Lemonade Stands: " + stands[1], 10, 52); 
     g.drawString("Cookie Stands: " + cookieStands, 10, 66); 
     g.drawString("Cupcake Stands: " + cupcakeStands, 10, 80); 
     g.drawString("Cost: " + costs[1], 355, 40); 
     g.drawString("Cost: " + cookieCost, 495, 40); 
     g.drawString("Cost: " + cupcakeCost, 620, 40); 
    } 

    public void run() {} 
    public void start() {} 
    public void stop() {} 
    public void destroy() {} 

    Timer t = new Timer(50, new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      money += income/50; //0.8 per second 
      repaint(); 
     } 
    }); 

    public void actionPerformed(ActionEvent e) { 
     /*if(e.getSource() == buyLemonade) { 
      if (money >= lemonadeCost) { 
      System.out.println("Lemonade stand bought. "); 
      income += 1; //0.4 actual 
      lemonadeStands++; 
      money -= lemonadeCost;      Replacing with method. 
      lemonadeCost += 4 * modifier; 
      modifier++; 
      }else System.out.println("Not enough money! "); 
     }*/ 
     buttonFunc(costs[1], 1, stands[1], 1, "Lemonade stand", 1); 
     if(e.getSource() == buyCookie) { 
      if (money >= cookieCost) { 
      System.out.println("Cookie stand bought. "); 
      income += 3; 
      cookieStands++; 
      money -= cookieCost; 
      cookieCost += 8 * modifier; 
      modifier += 2; 
      }else System.out.println("Not enough money! "); 
     } 
     if(e.getSource() == buyCupcake) { 
      if (money >= cupcakeCost) { 
      System.out.println("Cupcake stand bought. "); 
      income += 6; 
      cupcakeStands++; 
      money -= cupcakeCost; 
      cupcakeCost += 18 * modifier; 
      modifier += 3; 
      }else System.out.println("Not enough money! "); 
     } 
    } 

    public void buttonFunc(int cost, int incomeProduced, int stands, int modifierAmount, String name, int arrayLocation) { 
     if (money >= cost) { 
      System.out.println(name + " bought. "); 
      income += incomeProduced; 
      stands[arrayLocation] += 1;     //Where I get the error 
      money -= cost; 
      costs[arrayLocation] = (int) (costs[arrayLocation] + costs[arrayLocation] * 0.4); 
      modifier += modifierAmount; 
     }else System.out.println("Not enough money! "); 
    } 

    public void update (Graphics g) { 
     if (dbufferedImage == null) { 
       dbufferedImage = createImage(this.getSize().width, this.getSize().height); 
       dBufferedGraphic = dbufferedImage.getGraphics(); 
      } 
      dBufferedGraphic.setColor(getBackground()); 
      dBufferedGraphic.fillRect(0, 0, this.getSize().width, this.getSize().height); 

      dBufferedGraphic.setColor(getForeground()); 
      paint(dBufferedGraphic); 

      g.drawImage(dbufferedImage, 0, 0, this); 
    } 

} 

回答

1

您的方法中有一個名稱爲stands的正式參數,該參數隱藏了數組名稱。所以,在你的方法stands是指形式參數,這是一個int,這就是錯誤的原因。

要麼改變你的形參名稱,或使用this引用實例數組類型字段stands

this.stands[arrayLocation] += 1; 

而且,好像你甚至沒有使用你的方法stands參數。如果您打算傳遞數組出現,那麼你的方法簽名更改爲:

public void buttonFunc(int cost, int incomeProduced, int[] stands, ...) 

,然後調用此方法爲:

buttonFunc(costs[1], 1, stands, 1, "Lemonade stand", 1); // Change stands[1] to stands 

您也可以完全刪除該方法的參數。由於stands是一個實例引用變量,因此您可以從任何實例方法訪問它。此外,修改任何數組索引中的值將反映爲實際的數組對象。所以,只需擺脫stands參數。沒事的。

1

你傳入看臺[1],它是一個int值,而不是一個陣列reference.Also您接受int值,而不是陣列中的方法buttonFunc聲明:

public void buttonFunc(int cost, int incomeProduced, int stands, int modifierAmount, String name, int arrayLocation) 

由於看臺是一個int所以在array形式訪問它會導致錯誤的行:

 stands[arrayLocation] += 1;     //Where I get the error 

而且你好像我之間的混淆thod參數costcosts在類級別上定義的數組。

+0

爲什麼沒有成本'數組的問題,然後呢? – IHazABone

+0

@Iazazone您在全局數組的「成本」與方法輸入參數「成本」之間混淆不清。 –

相關問題