【发布时间】:2014-02-17 01:34:01
【问题描述】:
基本上,我一直很难使用 for 循环来查找存储在类天文台的数组列表中的最大地震震级。我收到一条错误消息,告诉我在我的 for 循环中找不到变量“i”,无法找到天文台记录的最大震级的方法。
class Observatory
{
private String name;
private String country;
private int yearStarted;
private int area;
private int runningTotal;
private ArrayList<Earthquake> earthquakes;
private ArrayList<String> observatories;
Scanner userInput = newScanner(System.in);
private double largestMag(ArrayList<Earthquake> earthquake, double magnitude)
{
if (earthquake == null || earthquake.isEmpty()){
return 0;
}
for (Earthquake quake : earthquake) {
if (earthquake.get(0) < earthquake.get(i)){
return earthquake.get(i);
}
if (earthquake.get(0) > earthquake.get(i)){
return earthquake.get(0);
}
}
}
我知道这与在我的地震类中添加地震的事实有关,数组列表存储了不止一条信息?
这是我的地震课
public class Earthquake
{
// instance variables - replace the example below with your own
private double magnitude;
private double latitude;
private double longitude;
private int yearOfEvent;
private String position;
private String details;
public Earthquake()
{
magnitude = 0;
latitude = 0;
longitude = 0;
yearOfEvent = 0;
}
public void setMagnitude(double magnitude)
{
this.magnitude= magnitude;
}
public double getMagnitude()
{
return magnitude;
}
public void setYear(int yearOfEvent)
{
this.yearOfEvent = yearOfEvent;
}
public int getYear()
{
return yearOfEvent;
}
public void setPosition(double latitude, double longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
public String getPosition()
{
position = latitude + ", " + longitude;
return position;
}
public String getDetails()
{
details = magnitude + " ," + latitude + "," + longitude + ","+ yearOfEvent;
return details;
}
}
我不确定如何解决从数组列表中提取大小以用于我的方法中的计算的问题。谢谢。
【问题讨论】:
-
也许您可以将
float用于magnitude?
标签: java arrays for-loop arraylist foreach