gpt4 book ai didi

java - 如何给数组中的对象赋值?

转载 作者:行者123 更新时间:2023-11-30 03:24:25 27 4
gpt4 key购买 nike

软件包第10章;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Customer {
private String name;
private String streetAddress;
private String phoneNumber;
private int total;

public String getName() {
return name;
}

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

public String getStreetAddress(){
return streetAddress;
}

public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}

public String getPhoneNumber() {
return phoneNumber;
}



public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public int getTotal(){
return total;
}

public void setTotal(int total){
this.total = total;
}

public static void assign(){
int a = (int) (Math.random() + 10);
int r = (int) (Math.random() + 10);
int f = (int) (Math.random() + 10);
System.out.println(a + r + f + "x" + "x" + "x");
}

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList <Customer > customerList = new ArrayList <Customer>();
char ans;
do
{
Customer customer = new Customer();
System.out.print("Customer name ");
customer.setName(in.next());
int i = 0;
++i;
System.out.print("Street Address ");
customer.setStreetAddress(in.next());

System.out.print("Phone Number ");
customer.setPhoneNumber(in.next());

customerList.add(customer);
System.out.println("Enter total sales ");
customer.setTotal(in.nextInt());

System.out.println("Would you like to enter in a new customer ( y/n)? ");
String answer = in.next();
ans = answer.charAt(0);
((String) customerList).concat("")

} while(ans == 'y');

for(Customer c: customerList){
System.out.print(c.getName() + "\n" + "Phone number is " +c .getPhoneNumber() +"\n" + "Total sales is "+ c.getTotal() + "\n" + "Address is"+ c.getStreetAddress());

}

for(int i=0; i<customerList.size(); i++){
//System.out.print(customerList.get(i).getName());
}
}
}

我需要为数组列表中的每个值分配一个数字,但收到一条错误,提示我必须转换为字符串(数组列表)。如何添加?

最佳答案

如果我从评论中收集到的信息是正确的,那么我相信这就是您想要的:

如果您想要随机值 1-10,则当前的 allocate() 是不正确的,它应该如下所示:

public String assign(){
Random rand = new Random();
int a = rand.nextInt(10) + 1;
int r = rand.nextInt(10) + 1;
int f = rand.nextInt(10) + 1;
return a+r+f+"xxx";
}

客户将如下所示:

public class Customer {
private String name;
private String customerNumber;
private String streetAddress;
private String phoneNumber;
private int total;
...
...
...
public String getCustomerNumber() { return this.customerNumber; }
public void setCustomerNumber(String cNumber) { this.customerNumber = cNumber; }

分配数字应该如下所示:

    for(Customer c : customerList) {
c.setCustomerNumber(assign());
}

还要避免这行代码,这是一个非常糟糕的主意:

    ((String) customerList).concat("")

您或许应该将 customerNumber 重命名为 customerID。

关于java - 如何给数组中的对象赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30610448/

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