我正在尝试创建一种类型的多个对象.然后我想将这些值传输到数组列表中.如何使用具有不同名称的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)
{ …Run Code Online (Sandbox Code Playgroud) 我试图将二维数组price [] []中的值(整数)放入数组seatArray [] []中对象的cost变量中.我认为问题在于我试图将价格数组中的值放入空中,因为seatArray数组只包含对null的对象引用.我该如何解决这个问题?
调用构造函数的行:
SeatChart seatArray = new SeatChart(givenArray);
Run Code Online (Sandbox Code Playgroud)
构造方法:
public SeatChart(int[][] prices)
{
Seat[][] seatArray = new Seat[9][10];
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 10; j++)
{
seatArray[i][j].cost=prices[i][j];
}
}
}
Run Code Online (Sandbox Code Playgroud)