2017-09-22 55 views
0

我想在java中製作一個程序,您可以添加人的生日,名字,birthmonths和birthyears。我希望能夠列出月份和每月出生人數的列表。我有以下代碼,2個類作爲「人」類和「分析器」類。這是下面的代碼,不能讓arraylist使用索引和while循環做一個列表

import java.util.*; 
/* 
* This project will keep track of which day 
* (1 to 31) and which month (1 to 12)in which 
* people are born. 
* Look to LogAnalyzer for clue. 
*/ 

public class Person 
{ 
private int birthDay; 
private int birthMonth; 
private int birthYear; 
private String name; 

public Person(String name, int birthDay, int birthMonth, int birthYear) 
{ 
    this.name = name; 
    if(birthDay >= 1 && birthDay <= 31){ 
     this.birthDay = birthDay; 
    } 
    else { 
     this.birthDay = -1; 
    } 

    if(birthMonth >= 1 && birthMonth <= 12){ 
     this.birthMonth = birthMonth; 
    } 
    else { 
     this.birthMonth = -1; 
    } 

    this.birthYear = birthYear; 

} 

public String getName() 
{ 
    return name; 
} 

public int getBirthDay() 
{ 
    return birthDay; 
} 

    public int getBirthMonth() 
{ 
    return birthMonth; 
} 

public int getBirthYear() 
{ 
    return birthYear; 
} 

public String toString() 
{ 
    return "Name: " + getName() + " BirthDay: " + getBirthDay() + 
    " BirthMonth: " + getBirthMonth() + " BirthYear: " + 
getBirthYear(); 
} 


} 

import java.util.ArrayList; 
import java.util.Iterator; 

public class Analyzer 
{ 
// instance variables - replace the example below with your own 
private int []birthDayStats; 
private int []birthMonthStats; 
private ArrayList people; 

/** 
* Constructor for objects of class Analyzer 
*/ 
public Analyzer() 
{ 
    people = new ArrayList(); 
} 

public void addPerson(String name, int birthDay, int birthMonth, int 
birthYear) 
{ 
    Person person = new Person(name, birthDay, birthMonth, birthYear); 
    if(person.getBirthDay()!=-1|| person.getBirthMonth() != -1) { 
     people.add(person); 
     birthMonthStats [birthMonth]++; 
     birthDayStats[birthDay]++; 
    } 
    else 
    { 
     System.out.println ("Your current Birthday is " + birthDay + " or " 
+ birthMonth + " which is not a correct number 1-31 or 1-12 please put in a 
correct number "); 

    } 


} 

public void printPeople() //prints all people in form: 「 Name: Tom 
Month: 5 Day: 2 Year: 1965」 
{ 
    int index = 0; 
    while(index < people.size()){  
     Person person = (Person) people.get(index);  
     System.out.println(person); 
     index++; 
    }   
} 

public void printMonthList()//prints the number of people born in each month 
Sample output to the right with days being similar 
{ 
    int index = 0; 
    while (index < birthMonthStats.length){ 
     System.out.println (birthMonthStats[index]); 
     index++; 
    } 
} 

}

我在遇到麻煩的代碼是「printmonthlist」方法,我試圖將其打印出來,但它不打印。我希望它能打印出12個月的月份清單和每個月出生的人數。如果你們中的任何人都能幫我弄清楚謝謝。

+0

順便說一句,你可以使用['LocalDate'](https://docs.oracle.com/javase/8/ docs/api/java/time/LocalDate.html)日期類而不僅僅是整數。 'LocalDate.of(y,m,d)'和['YearMonth'](https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html)&['MonthDay' ](https://docs.oracle.com/javase/8/docs/api/java/time/MonthDay.html)類可能會有用。 –

回答

0

你忘了初始化你的數組。正如你在我的代碼中看到的,我在Analyzer創建中用兩個常量初始化了數組。 注:爲便於閱讀,我只是改變了一點點你的日誌中printMonthList()方法

import java.util.ArrayList; 

public class Analyzer 
{ 
    // instance variables - replace the example below with your own 
    private final static int DAYS_PER_MONTH = 31; 
    private final static int MONTHS_PER_YEAR = 12; 
    private int []birthDayStats; 
    private int []birthMonthStats; 
    private ArrayList<Person> people; 

    /** 
    * Constructor for objects of class Analyzer 
    */ 
    public Analyzer() 
    { 
     this.people = new ArrayList<Person>(); 
     // A default value of 0 for arrays of integral types is guaranteed by the language spec 
     this.birthDayStats = new int[Analyzer.DAYS_PER_MONTH]; 
     // A default value of 0 for arrays of integral types is guaranteed by the language spec 
     this.birthMonthStats = new int[Analyzer.MONTHS_PER_YEAR]; 
    } 

    public void addPerson(String name, int birthDay, int birthMonth, int 
    birthYear) 
    { 
     Person person = new Person(name, birthDay, birthMonth, birthYear); 
     if(person.getBirthDay()!=-1|| person.getBirthMonth() != -1) { 
      people.add(person); 
      birthMonthStats [birthMonth-1]++; 
      birthDayStats[birthDay-1]++; 
     } 
     else 
     { 
      System.out.println ("Your current Birthday is " + birthDay + " or " 
    + birthMonth + " which is not a correct number 1-31 or 1-12 please put in a correct number "); 

     } 


    } 

    public void printPeople() //prints all people in form: 「 Name: Tom Month: 5 Day: 2 Year: 1965」 
    { 
     int index = 0; 
     while(index < people.size()){  
      Person person = (Person) people.get(index);  
      System.out.println(person); 
      index++; 
     }   
    } 

    public void printMonthList()//prints the number of people born in each month Sample output to the right with days being similar 
    { 
     int index = 0; 
     while (index < birthMonthStats.length){ 
      System.out.println ("Month number " + (index+1) + " has " + birthMonthStats[index] + " people"); 
      index++; 
     } 
    } 
} 
+0

現在,如果我想看看哪個月是最流行的,我該怎麼做?我使用了public void mostPopularMonth(),但我不知道從哪裏開始下一步 – Stevo4586

+0

您應該遍歷'birthMonthStats'來查找最大值並保存您找到它的索引。那是你的月份 – rakwaht