Java 表达式的类型必须是数组类型,但它解析为int-与有效数组的语法相同?

Java 表达式的类型必须是数组类型,但它解析为int-与有效数组的语法相同?,java,arrays,Java,Arrays,我有两个数组,都是int类型。costs[]数组工作得很好,没有错误或bug,但是站立的数组编码完全相同,在我在程序底部附近定义的名为buttonFunc的方法中,给出了错误“表达式的类型必须是数组类型,但它解析为int”。在init()方法中,我调用stands[1]=0,效果很好,但当我做了几乎相同的事情(用调用该方法时定义的整数替换1)时,就会出现错误。为什么两个语法和用法完全相同的数组可以这样做 `import java.applet.Applet; import java.awt.Bo

我有两个数组,都是
int
类型。
costs[]
数组工作得很好,没有错误或bug,但是
站立的
数组编码完全相同,在我在程序底部附近定义的名为
buttonFunc
的方法中,给出了错误“表达式的类型必须是数组类型,但它解析为int”。在
init()
方法中,我调用
stands[1]=0
,效果很好,但当我做了几乎相同的事情(用调用该方法时定义的整数替换1)时,就会出现错误。为什么两个语法和用法完全相同的数组可以这样做

`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);
    }

}

您正在传递stands[1],它是一个int值而不是数组引用。此外,您在方法
buttonFunc
声明中接受的是一个int值而不是数组:

public void buttonFunc(int cost, int incomeProduced, int stands, int modifierAmount, String name, int arrayLocation)
As stands是一个
int
,因此在
数组中访问它将导致行中出现错误:

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

此外,您似乎混淆了类级别上定义的方法param
cost
cost
数组。

您的方法中有一个名为
的形式参数,它隐藏了数组名。因此,在您的方法中,
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
参数。这会很好。

为什么
costs
数组没有问题呢?@IHazABone您正在混淆全局数组的“costs”和方法输入参数“cost”。