-2

问题:我的数组有什么问题,我该如何解决?

细节:我在main方法中初始化了数组,值在一个方法中设置。我用第二种方法调用了数组值,一切都很好。当我尝试在第三种方法中调用数组时,我得到了越界错误,即使数组的大小完全相同。我试图调用数组以复制它,然后对第二个数组进行排序。

谢谢你

private static  WeatherLocation[] WeatherSpots = new WeatherLocation[6];
private static Scanner Input = new Scanner(System.in);

public static void main(String[] args) 
{int Count;

for(Count = 0 ; Count < 6; Count++)
    WeatherSpots[Count] = new WeatherLocation();

WeatherSpots[0].LocationID = "Tciitcgaitc";
WeatherSpots[1].LocationID = "Redwood Haven";
WeatherSpots[2].LocationID = "Barrier Mountains";
WeatherSpots[3].LocationID = "Nina's Folly";
WeatherSpots[4].LocationID = "Scooly's Hill";
WeatherSpots[5].LocationID = "Twin Cones Park";
    SetUp();

    String Command = "";
    while(!Command.equals("Quit"))  {
    Menu();

    System.out.print("Enter Command: ");
    Command = Input.nextLine();

    if(Command.equals("Post"))
        PostTemperatureInfo();
    if(Command.equals("Daily"))
        WeeklyReport();
    else if (Command.equals("HighLow"))
        Sorting();
    }
}

public static void PostTemperatureInfo()
{
    Scanner LocalInput = new Scanner(System.in); 
    int K;
    int Temp;
    //...then get the values for each location...

    System.out.println( "Enter the Temperature for each weather station below:\n");
    System.out.println( "---------------------------------------------------------------");

    for(K = 0 ; K < 6 ; K++)  {
        System.out.println( "Weather Station: " + WeatherSpots[K].LocationID);  //Display the location of the fishing spot...

        System.out.print( "Enter Temperature:\t");   //Get the count...
        Temp = LocalInput.nextInt();
         System.out.println( "---------------------------------------------------------------");
         WeatherSpots[K].CatchCount = Temp;
        }
    System.out.println("");
     System.out.println("");
     System.out.println("");
}
public static void WeeklyReport()
{
    for(K = 0 ; K < 6 ; K++) 
        {System.out.println( "" + WeatherSpots[K].LocationID +"\t\t" + WeatherSpots[K].CatchCount + "\t\t" + String.format("%.2f", (WeatherSpots[K].CatchCount - 32) * 5 / 9));
}
}

public static void Sorting()
{int K = 0; 

for(K = 0 ; K < 6 ; K++);
    {int [] copycat = new int[K];


    System.arraycopy(WeatherSpots[K].CatchCount, 0, copycat[K], 0, 6);
    System.out.println("" + copycat[K]);
    Arrays.sort(copycat, 0, K);
    System.out.println("Minimum = " + copycat[0]); 
System.out.println("Maximum = " + copycat[K -1]);  

        }
    }
}
4

2 回答 2

1

问:为什么不使用“array.length”而不是硬编码的“6”?

问:如果可以避免的话,我真的不鼓励你使用这种缩进样式。

无论如何 - 这应该有效(我自己没有尝试过):

public static void Sorting() {
  for(int K = 0 ; K < WeatherSpots.length ; K++) {
     int [] copycat = new int[K];

     System.arraycopy(
       WeatherSpots[K].CatchCount, 0, copycat[K], 0, WeatherSpots.length);
     System.out.println("" + copycat[K]);
     Arrays.sort(copycat, 0, K);
     System.out.println("Minimum = " + copycat[0]); 
     System.out.println("Maximum = " + copycat[K -1]);  
  }
}

最主要的是去掉多余的“;” 在“for()”循环之后。

于 2012-06-24T04:27:06.907 回答
1

问题是您正在分配一个copycat只有K整数长的数组,然后您尝试将 6 个元素放入其中,即使 K == 0 也是如此。我对您的代码了解不够,无法弄清楚正确的索引是什么,但这是你问题的根源。

实际上,我不相信您发布的代码会编译。此行来自Sorting()

System.arraycopy(WeatherSpots[K].CatchCount, 0, copycat[K], 0, 6);

似乎非常可疑。的第一个和第三个参数System.arraycopy应该是数组,但是copycat[K]是一个int. 显然是这样WeatherSpots[K].CatchCount

编辑:

从您的评论和代码看来,该Sorting()例程应该只打印WeatherSpots[K].CatchCount. 这可以比你做的容易得多。这是一种方法:

public static void Sorting() {
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for (WeatherLocation loc : WeatherSpots) {
        final int count = loc.CatchCount;
        if (count < min) {
            min = count;
        }
        if (count > max) {
            max = count;
        }
    }
    System.out.println("Minimum = " + min); 
    System.out.println("Maximum = " + max);
}
于 2012-06-24T04:39:00.637 回答