gpt4 book ai didi

java - 打印一维数组

转载 作者:太空宇宙 更新时间:2023-11-04 15:00:26 25 4
gpt4 key购买 nike

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

例如:

输入客户总数:3

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


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


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

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

姓名-------------年龄------------性别

迈克                 35                  男

迈克                 35                  男

迈克                35                  男

相反,我想要得到类似的东西:

姓名-------------年龄------------性别

杰西卡             22                  女

约翰                 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;
}



}
}

最佳答案

您的问题是在 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;
}



}

关于java - 打印一维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22622403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com