簡體   English   中英

數組和ArrayClass列表的總和

[英]Sum of an Array & ArrayClass List

這是您要求的Die類... Alex您可以進一步解釋在我的Die類中使用.getValue嗎?


public class Die
{
   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;
    }
}

我嘗試使用不同的代碼來查找Array和ArrayList的總和...這是我的代碼。 有人可以告訴我如何解決問題。

import java.util.ArrayList;


public class Driver {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final int SIZE = 10;
        Die sumArray;
        Die sumArrayList;


        Die[] DieList = new Die[SIZE];

        ArrayList<Die> DieList2 = new ArrayList<Die>();

        for (int i = 0; i < SIZE; i++)
        {
            DieList[i] = new Die();
        }

        for (int i = 0; i < SIZE; i++)
        {
            DieList2.add(new Die());
        }

        for (int i = 0; i < SIZE; i++)
        {
            DieList[i].roll();
        }

        for (int i = 0; i < SIZE; i++)
        {
            DieList2.get(i).roll();
        }

        System.out.print("Array: "); 

        for (int i = 0; i < SIZE; i++)
        {
        System.out.print(DieList[i] + " ");
        }

        System.out.println();

        System.out.println("ArrayList: " + DieList2); //.get(index) specific index value in the arrayList

        for (Die i : sumArray){
            sumArray+= i;
        }

    //  sumArrayList = (DieList2(0) + DieList[1] + DieList[2] + DieList[3] + DieList[4] + DieList[5] + DieList[6] + DieList[7] + DieList[8] + DieList[9] + DieList[10]);

    }
}

我發現您誤解了一些概念:

  • Object類(所有類均從其繼承的類)中定義的print方法將打印對象的引用,而不是任何屬性。 這應該是顯而易見的,因為print方法無法知道要打印哪個屬性(屬性和引用的列表都沒有存儲在對象中一個接一個地移動)。 如果需要打印其內部屬性,則需要專門編寫一個方法來執行此操作或覆蓋print方法,但是如果您沒有充分的理由要覆蓋預定義的方法,則不要這樣做。 稱之為getValue()即

  • sumArray只是一個對象,而不是對象列表。 您遍歷第一個for循環的方式是僅讀取一個對象,因為sumArray是您的Die對象之一,而不是列表。

  • 除字符串外,運算符+僅用於基本類型(int,boolean,float等)。 如果+ =不會破壞您的編譯,我理解編譯器會將操作解釋為單個賦值操作,因此您是說引用sumArray現在再次指向... sumArray。 該聲明毫無用處。

  • 至於注釋的最后一行,由於上述原因,您不能對對象求和。 對象是由許多屬性和方法組成的復雜實體,並且編譯器默認情況下不知道如何求和。 如果出於某種原因從C ++到Java,請注意您不能重載運算符(但肯定不是您的情況)

所以,如果您想要一個解決方案

  • 在Die類中定義一個getValue()方法以讀取存儲的值。 我假設您已經正確編碼了roll值以生成一個值並將其存儲。

要對數組中的所有值求和,請執行以下操作:

int sums1=0;    
for (Die i : DieList){
sums1+= i.getValue();
}

要對ArrayList中的所有值求和:

int sums2=0;    
for (int i=0;i<DieList2.size();i++){
     sums2+= DieList2.get(i).getValue();
}

最后打印它們:

System.out.println("Sum of array elements: "+sums1);
System.out.println("Sum of ArrayList elements: "+sums2);

如果您只是在擲骰子后試圖查找數組的總和,則可能會大大縮短代碼!

您可以像這樣在一個循環中壓縮骰子的所有添加,滾動和求和。

public static void main(String[] args) {
    final int SIZE = 10;
    int sumList1 = 0;
    int sumList2 = 0;
    Die[] DieList = new Die[SIZE];
    ArrayList < Die > DieList2 = new ArrayList < Die > ();

    for (int i = 0; i < SIZE; i++) {
        DieList[i] = new Die();
        DieList2.add(new Die());
        DieList[i].roll();
        DieList2.get(i).roll();
        sumList1 += DieList[i].getFaceValue();
        sumList2 += DieList2.get(i).getFaceValue();
    }

    System.out.println("Sum of Array: " + sumList1);
    System.out.println("Sum of ArrayList: " + sumList2);
    System.out.println("Sum of both: " + (sumList1 + sumList2));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM