2014-10-17 52 views
1

大家好我是从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年的预订数据阿鲁巴。

+3

时间做一些调试我认为 – 2014-10-17 08:53:04

+0

您可以删除这两个组方法此构造函数'公共SubscriptionYear(INT年,双订阅)'调用。他们是不必要的。 – Tom 2014-10-17 09:03:53

+1

而你的'public void addSubscriptionYear(int year,double subscription)'方法看起来很奇怪。它看起来像是用新的覆盖现有的'订阅'条目。现在,我不惊讶你得到错误的结果。 – Tom 2014-10-17 09:08:04

回答

1

正如汤姆在评论中指出的那样 - 问题在于addSubscriptionYear由于某种原因将subscriptions数组中的每个条目都替换为最新添加的条目。再次,援引汤姆,该解决方案可以使用相同的algorythm如您在getNumSubscriptionsForPeriod方法应用于:

subscriptions[subscriptions[0].getYear()-year] = new SubscriptionYear(year, subscription); 

,当然还有下降的循环完全。

你也可以放弃奇怪的索引,并总是去通过整个阵列。出于某种原因,您在SubscriptionYear中拥有该年份参数。另外,只需在下一个空白处添加SubscriptionYear,然后在通过整个数组并在当前SubscriptionYear处于边界内时添加总和。

但是,使用年份作为关键字和订阅作为每个国家/地区的值的地图将比使用获取正确索引的神秘algorythm的数组包围头更简单。比你只需:

Map<Integer,Double> subscriptions = new HashMap<Integer,Double>(); 
public void addSubscriptionYear(int year, double subscription) 
{ 
    subscriptions.put(year,subscription); 
} 
public double getNumSubscriptionsForPeriod(int start, int end) 
{ 
    double sum = 0; 
    for(int i=start;i<=end;i++){ 
    sum += subscriptions.get(year); 
    } 
    return sum; 
} 
+0

我不允许使用地图。实例变量叫做“subscriptions”,是SubscriptionYear类型的一维数组,它保存着该国家的所有订阅数据。一个构造函数,它接受国家名称和年数,我们将用它来初始化我们的“订阅“array.addSubscriptionYear”方法需要在int类型的年份和类型为double的单一订阅中使用它来创建一个新的SubscriptionYear对象并将其保存在“订阅”数组中 – user3497437 2014-10-17 09:31:45

+0

这是一个人为的限制,如果这是一个标准问题,Map是正确的答案,但是由于这显然是一个编程练习,所以我不打算拼出答案,我(通过Tom)指出什么是错的,什么是可能的解决方法,现在是你的工作。至少尝试编辑你的问题,遇到使用system.out.println(订阅[0] .getYear())时遇到的下一个问题 – Deltharis 2014-10-17 09:37:39

+0

;它会打印出1960年的年份o 2012重复并覆盖数据。我只需要一个指导方针,而我是编程新手。我的错误似乎来自存储数据。 – user3497437 2014-10-17 09:54:47

0

你的问题是在方法:

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"); 

} 

这种方法创建每次申购的阵列相同的值。

您应该(在leas上)使用列表而不是数组并使用.add()方法,或者使用年份为hashmaps作为索引。

D.

+0

我不允许使用地图。实例变量叫做“subscriptions”,是SubscriptionYear类型的一维数组,它保存着该国家的所有订阅数据。一个构造函数,它接受国家名称和年数,我们将用它来初始化我们的“订阅“array.addSubscriptionYear”方法需要在int类型的年份和类型为double的单个订阅中使用它创建一个新的SubscriptionYear对象并将其保存在“subscriptions”数组中。 – user3497437 2014-10-17 09:33:26