【问题标题】:the operator += is undefined Java array运算符 += 是未定义的 Java 数组
【发布时间】:2015-12-10 04:25:20
【问题描述】:

这里是我的第一个Array类,只是制作了一个简单的Die数组和Array列表。

public class Die
{
    private int total;
   private final int MAX = 6;  // maximum face value

   private int faceValue;  // current value showing on the die

   //-----------------------------------------------------------------
   //  Constructor: Sets the initial face value.
   //-----------------------------------------------------------------
   public Die()
   {
      faceValue = 1;
   }

   //-----------------------------------------------------------------
   //  Rolls the die and returns the result.
   //-----------------------------------------------------------------
   public int roll()
   {
      faceValue = (int)(Math.random() * MAX) + 1;

      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Face value mutator.
   //-----------------------------------------------------------------
   public void setFaceValue(int value)
   {
      faceValue = value;
   }

   //-----------------------------------------------------------------
   //  Face value accessor.
   //-----------------------------------------------------------------
   public int getFaceValue()
   {
      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Returns a string representation of this die.
   //-----------------------------------------------------------------
   public String toString()
   {
      String result = Integer.toString(faceValue);

      return result;
   }

}

接下来是数组和数组列表,我在操作员尝试检索数组的总数时遇到问题。

import java.util.ArrayList;

public class ArrayWithDice {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Die dieArray[] = new Die[10];
        ArrayList<Die> set1 = new ArrayList<Die>();


        for(int i=0; i<dieArray.length; i++)
        {
            dieArray[i] = new Die();
        }
        for (int i=0; i<dieArray.length; i++)
        {
            dieArray[i].roll();
            System.out.println(dieArray[i]);
        } 
        int total = 0;
        for(int i=0; i <dieArray.length; i++)
        {
            total += dieArray[i];
        }
        System.out.println("Total: " + total);


        for (int i = 0; i<10; i++)
        {
            set1.add(i, new Die());
        }
        for (int i = 0; i <set1.size(); i++)
        {
            set1.get(i).roll();
        }
        for (int i = 0; i <set1.size(); i++)
        {
            System.out.println(set1.get(i));
        }
        int total1 = 0;
        for(int i=0; i <set1.size(); i++)
        {
            total1 += set1.size();
        }
    }
}

谁能向我解释为什么我会收到这个错误?除了修改原始类之外,我已经尝试了大多数我能想到的东西,之后我将比较数组和数组列表之间的总数,但我相信我可以自己编写这部分代码。谢谢。

【问题讨论】:

  • 你的意思是total += dieArray[i].getFaceValue()?
  • 请包含确切的错误信息并指向它所指的源代码行。
  • 最后一个循环没有意义。它正在计算int total1 = set1.size() * set1.size();。你的意思是total1 += set1.get(i).getFaceValue();?
  • 为什么不使用 total = total + dieArray[i].getFaceValue();

标签: java undefined operator-keyword


【解决方案1】:

您正在尝试将 Die 对象添加到整数。

也许你想要

total += dieArray[i].getFaceValue ();

【讨论】:

    【解决方案2】:

    无论你在哪里

    total += dieArray[i];
    

    放

    total += dieArray[i].getFaceValue();
    

    我认为您的问题是您正在尝试将类型 Die 添加到 int。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-17
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      相关资源
      最近更新 更多