2014-10-17 21 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