打印1D阵列

时间:2014-03-24 23:05:05

标签: java arrays printing

我试图从另一个类打印一维数组。但问题是print语句只打印相同的元素。

例如:

输入客户总数:3

姓名:杰西卡
年龄:22岁
性别:女性


姓名:约翰
年龄:28岁
性别:男性


姓名:迈克
年龄:35岁
性别:男性

我从这个程序得到的输出与最后一个客户的元素相同。

名称------------- -----------年龄性别

迈克35男

迈克35男

迈克35男

相反,我希望得到类似的东西:

名称------------- -----------年龄性别

杰西卡22女

John 28男

迈克35男

我不知道我错过了什么部分或做错了什么。我真的很感激任何反馈!

import java.util.Scanner;
public class Customer1234 {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    int x;
    System.out.print("Enter total number of customers: ");
     x = input.nextInt();

    Customer [] customers = new Customer[x];



    Customer.data(customers);

    System.out.println("Name" + "-------------"+ "Age" +"-----------" +"Gender");
    for(int i = 0; i <customers.length; i++){

        System.out.println(customers[i].toString());
    }



}

static class Customer{

    private static String name;
    private static int age;
    private static String gender;


    public Customer(String name, int age, String gender){

        Customer.setName(name);
        Customer.setAge(age);
        Customer.setGender(gender);

    }


    public static void data(Customer [] customers){
        Scanner input = new Scanner(System.in);

        for(int i = 0; i < customers.length; i++){

            System.out.print("Name: ");
            setName(input.next());

            System.out.print("Age: ");
            setAge(input.nextInt());

            System.out.print("Gender: ");
            setGender(input.next());

            customers[i] = new Customer( getName(), getAge(), getGender());
        }

    }


    public static String getName() {
        return name;
    }

    public static void setName(String name) {
        Customer.name = name;
    }

    public static int getAge() {
        return age;
    }

    public static void setAge(int age) {
        Customer.age = age;
    }

    public static String getGender() {
        return gender;
    }

    public static void setGender(String gender) {
        Customer.gender= gender;
    }





    public String toString()
    {
        String result;
        result = name +"         "+ age +"            "+ gender;
        return result;
    }



}
}

2 个答案:

答案 0 :(得分:0)

您使用的static变量只为Customer类的每个字段允许一个值。创建所有字段实例变量,以确保每个类的实例使用单独的值

class Customer{

    private String name;
    private int age;
    private String gender;

    ...
}

答案 1 :(得分:0)

您的问题是在Customer类中到处使用static。我会把它重写为:

 private String name;
    private int age;
    private String gender;


    public Customer(String name, int age, String gender){

        this.name = name;
        this.age = age;
       this.gender = gender;

    }


    public static void data(Customer [] customers){
        Scanner input = new Scanner(System.in);

        for(int i = 0; i < customers.length; i++){

            System.out.print("Name: ");
            setName(input.next());

            System.out.print("Age: ");
            setAge(input.nextInt());

            System.out.print("Gender: ");
            setGender(input.next());

            customers[i] = new Customer( getName(), getAge(), getGender());
        }

    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender= gender;
    }





    public String toString()
    {
        String result;
        result = name +"         "+ age +"            "+ gender;
        return result;
    }



}