簡體   English   中英

在具有相同值的另一個循環中再次使用在循環中創建的數組

[英]Using An Array Created In a loop again in another loop with Same Value

    double[] IndividualAccumulatesArray; 

    for (int i=1;i<=Employees;i++)
    {
        IndividualAccumulatesArray = new double[i];

        System.out.println("\n");
        System.out.print("What is the Accumulated Earning's for Employee #"+i+"?   $");
        IndividualAccumulatesArray[i] = reader.nextDouble();

所以我在for循環中創建了IndividualAcculates數組但是我想保持每個數組的值,以便它可以在另一個for循環中使用

while(Confirm.equalsIgnoreCase("yes"))
    {
        for (int i=1;i<=Employees;i++)
        { 
            System.out.println("\n");
            System.out.print("How Much Dose Employee #"+i+" Get This Pay Period?   $");
            PayCheck = reader.nextDouble();

            if (IndividualAccumulatesArray[i]>=7000)
            {

因為它表示可能沒有為第二個for循環初始化變量IndividualAccumulatesArray

如果要創建一個您不知道初始大小的動態數組。

請改用ArrayList

現在的問題是,在進入for循環之前,你沒有初始化數組。 這意味着它可能永遠不會被初始化。

如果數組的大小需要是Employees的數量。

  double[] IndividualAccumulatesArray = new double[Employees]; 

  for (int i=1;i<=Employees;i++)
  {  
    System.out.println("\n");
    System.out.print("What is the Accumulated Earning's for Employee #"+i+"?   $");
    IndividualAccumulatesArray[i] = reader.nextDouble();

您只聲明變量但不要初始化數組,只需使用Employees初始化它,如下所示:

double[] IndividualAccumulatesArray = new double[Employees];

暫無
暫無

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

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