大家好我是从csv文件中读取一个字符串,年和双。基本上阅读国家名称,年份和蜂窝数据统计。例如:从参考数组中输出错误的总和
Country Name 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
Aruba 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.029310471 0 0 2.138784453 3.605985937 3.98141538 6.16435217 13.48254011 16.50927821 57.05427692 65.05605558 72.10431377 99.64250268.
我的程序能够读取数据,但输出错误的总和到屏幕上。我创建了2个课程。一个名为subscriptionYear的变量名为year(存储订阅数据的年份)和订阅(存储特定年份的订阅数)。被称为国家的第二类存储来自每个国家的国家和订阅数据。我的方法getNumSubscriptions计算错误,因为它只读取了2012年的内容,而没有执行总计。如果我输入其他年份它读取总和为0,只读过去年的2012年。我怎样才能计算总和使用一年的指数位置计算1960年至2012年之间的总和。请有人可以告诉我我做错了什么。
public class SubscriptionYear {
private int year;
private double subscriptions;
public SubscriptionYear(int year,double subscriptions)
{
this.year = year;
this.subscriptions = subscriptions;
setYear(year);
setSubscription(subscriptions);
}
public void setYear(int Year)
{
this.year= Year;
}
public void setSubscription(double value)
{
this.subscriptions = value;
}
public int getYear()
{
return year;
}
public double getSubscription()
{
return subscriptions;
}
public String toString()//returns number of subscriptions
{
return "Number of Subscriptions: "+subscriptions;
}
}
类国家:
public class Country {
private String countryNames;
private SubscriptionYear[] subscriptions;
private int size;
public Country(String country, int arraylength)
{
this.countryNames = country;
this.size = arraylength;
subscriptions = new SubscriptionYear[size];
}
public void addSubscriptionYear(int year, double subscription)
{
for(int i=0;i<subscriptions.length;i++)
{
subscriptions[i] = new SubscriptionYear(year, subscription);
}
System.out.print(subscriptions[0].getYear()+"\t");
}
public double getNumSubscriptionsForPeriod(int start, int end)
{
double sum =0;
int head = subscriptions[0].getYear()-start;
int tail = end-start;
for(int k=head;k<=tail;k++)
{
sum += subscriptions[k].getSubscription();
}
return sum;
}
}
TEST FILE:
Country [] countries;
//countries = new Country[NUM_COUNTRIES_TO_TEST]; // Note: Use this for initial testing of your implementation.
countries = new Country[countryNames.length]; //READS 253 COUNTRIES
Country current;
for (int countryIndex = 0; countryIndex < countries.length; countryIndex++)
{
int numberOfYears = yearLabels.length; // READS THE YEAR BTWN 1960 AND 2012
current = new Country(countryNames[countryIndex], numberOfYears); //CALLS CONSTRUCTOR
for (int yearIndex = 0; yearIndex < numberOfYears; yearIndex++)
{
double [] allSubscriptions = parsedTable[countryIndex];
double countryData = allSubscriptions[yearIndex];
current.addSubscriptionYear(yearLabels[yearIndex], countryData); //STORES THE YEAR AND SUBSCRIPTION DATA OF EACH YEAR
}
countries[countryIndex] = current;
}
System.out.printf(countryNames[0] + " (1960 to 2012): %.2f \n", countries[0].getNumSubscriptionsForPeriod(1960,2012));
// the output is: Aruba (1960 to 2012): 1170.50
应该输出作为1170.50总和但它只输出131.86其是2012年的预订数据阿鲁巴。
时间做一些调试我认为 – 2014-10-17 08:53:04
您可以删除这两个组方法此构造函数'公共SubscriptionYear(INT年,双订阅)'调用。他们是不必要的。 – Tom 2014-10-17 09:03:53
而你的'public void addSubscriptionYear(int year,double subscription)'方法看起来很奇怪。它看起来像是用新的覆盖现有的'订阅'条目。现在,我不惊讶你得到错误的结果。 – Tom 2014-10-17 09:08:04