带有一组选项的代码,用于打印联系人列表,联系人数量,联系人的平均年龄以及每种联系人的数量

时间:2016-02-07 18:45:38

标签: java arrays

我在这段代码中有3个选项,据我所知,我的其他类都设置正确。选项1和3似乎工作正常,但选项2我希望它打印联系人的数量(它已经是),打印每种联系人的数量,即个人或商业,然后打印所有的平均年龄联系人。我无法弄清楚如何获得平均年龄或如何打印每种类型。

import java.util.Scanner;

public class PlannerMain {     static Sc​​anner scanner = new Scanner(System.in);

public static void main(String[] args) {

    Contact[] contacts = new Contact[6];
    contacts[0] = new PersonalContact("Joe Smith, ", 33, ", 100 Evergreen Ave, ", "Seattle,", "WA ", 98999); // Personal
    contacts[1] = new PersonalContact("Lawrence Williams, ", 45, ", 2000 1st St, ", "Tacoma,", "WA ", 98100); // Personal
    contacts[2] = new PersonalContact("Rachel Garcia, ", 12, ", 12 Forest Drive, ", "Los Angelos,", "CA ", 99301); // Personal
    contacts[3] = new BusinessContact("Gregory Smith, ", 67, ", 360-888-7777", " 360-555-6666"); // Business
    contacts[4] = new BusinessContact("Jerome Bradley, ", 18, ", 216-111-2222", " 253-444-7777"); // Business
    contacts[5] = new BusinessContact("Susie Adams, ", 23, ", 253-333-7777", " 425-666-9999"); // Business

    while (true) {

        System.out.println("1.Print planner contacts. ");
        System.out.println("2.Print planner statistics. ");
        System.out.println("3.Exit.");

        int option = scanner.nextInt();

        if (option == 1) { // Displays every contact in the list until the length of the list has been reached

            for (int i = 0; i < contacts.length; i++) {
                System.out.println(contacts[i]);

            } // End for
        } // End option 1

        else if (option == 2) { 
            int sum = 0;
            System.out.println("Number of contacts: " + contacts.length);

            for (int i = 0; i < contacts.length; i++){
                sum += contacts[i];
                }
            System.out.println("Average age: " + (sum / contacts.length));

              if(contacts[] instanceof PersonalContact){             
                for (int i = 0; i < PersonalContact.getLength(); i++){
                    System.out.println("Personal Contacts: " + personalContacts.length);
                }// End for PC
               }//End if PC

              if(contacts[] instanceof BusinessContact){             
                for (int i = 0; i < BusinessContact.getLength(); i++){
                    System.out.println("BusinessContacts: " + businessContacts.length);
                }// End for BC
               }//End if BC

        } // End option 2

        else if (option == 3) { /** Terminates the program */
            System.exit(0);
        } // End option 3
    } // End while


}// End void main

} //结束

1 个答案:

答案 0 :(得分:1)

要获得目前的平均年龄,您使用的是sum变量,并尝试为联系人中的每个联系人添加联系人[i]。您真正想要的是使用类似

之类的东西将每个联系人的年龄添加到总和中
sum += contacts[i].getAge()

sum += contacts[i].age

取决于Contact类的实现方式。

要获取PersonalContacts和BusinessContacts的数量,您可以执行以下操作:

int personalContactsCount = 0;
for (int i = 0; i < contacts.length; i++) {
    if (contacts[i] instanceof PersonalContact) {
        personalContactsCount++;
    }
}  
int businessContactsCount = contacts.length - personalContactsCount;