use*_*907 10 java arraylist object while-loop
我正在尝试创建一种类型的多个对象.然后我想将这些值传输到数组列表中.如何使用具有不同名称的while循环创建对象.例如,这里是我的代码,但它只会生成一个名称的对象.
Customer cust = new Customer("bob", 20.0);
Run Code Online (Sandbox Code Playgroud)
和我的构造函数,如果你想看到:
public Customer(String customerName, double amount)
{
String name=customerName;
double sale=amount;
}
Run Code Online (Sandbox Code Playgroud)
StoreTest类(使用main方法):
import java.util.ArrayList;
import java.util.Scanner;
public class StoreTest {
ArrayList<Customer> store = new ArrayList<Customer>();
public static void main (String[] args)
{
double sale=1.0; //so the loop goes the first time
//switch to dowhile
Scanner input = new Scanner(System.in);
System.out.println("If at anytime you wish to exit" +
", please press 0 when asked to give " +
"sale amount.");
while(sale!=0)
{
System.out.println("Please enter the " +
"customer's name.");
String theirName = input.nextLine();
System.out.println("Please enter the " +
"the amount of the sale.");
double theirSale = input.nextDouble();
store.addSale(theirName, theirSale);
}
store.nameOfBestCustomer();
}
}
Run Code Online (Sandbox Code Playgroud)
客户类:
public class Customer {
private String name;
private double sale;
public Customer()
{
}
public Customer(String customerName, double amount)
{
name=customerName;
sale=amount;
}
}
Run Code Online (Sandbox Code Playgroud)
商店类(有解决arraylist的方法:
import java.util.ArrayList;
public class Store {
//creates Customer object and adds it to the array list
public void addSale(String customerName, double amount)
{
this.add(new Customer(customerName, amount));
}
//displays name of the customer with the highest sale
public String nameOfBestCustomer()
{
for(int i=0; i<this.size(); i++)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
nhg*_*rif 12
ArrayList<Customer> custArr = new ArrayList<Customer>();
while(youWantToContinue) {
//get a customerName
//get an amount
custArr.add(new Customer(customerName, amount);
}
Run Code Online (Sandbox Code Playgroud)
为了这个工作......你将不得不修复你的构造函数......
假设你的Customer类有变量叫做name和sale,你的构造应该是这样的:
public Customer(String customerName, double amount) {
name = customerName;
sale = amount;
}
Run Code Online (Sandbox Code Playgroud)
将您的Store课程更改为更像这样的内容:
public class Store {
private ArrayList<Customer> custArr;
public new Store() {
custArr = new ArrayList<Customer>();
}
public void addSale(String customerName, double amount) {
custArr.add(new Customer(customerName, amount));
}
public Customer getSaleAtIndex(int index) {
return custArr.get(index);
}
//or if you want the entire ArrayList:
public ArrayList getCustArr() {
return custArr;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此代码...
public class Main {
public static void main(String args[]) {
String[] names = {"First", "Second", "Third"};//You Can Add More Names
double[] amount = {20.0, 30.0, 40.0};//You Can Add More Amount
List<Customer> customers = new ArrayList<Customer>();
int i = 0;
while (i < names.length) {
customers.add(new Customer(names[i], amount[i]));
i++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87640 次 |
| 最近记录: |