在Java中的for循环中创建几个新对象

时间:2012-05-10 22:04:39

标签: java

我想从for循环中的类创建多个对象。但我不知道如何编码。我写的内容创建了一个新对象,但它覆盖了前一个对象。

package assginment1_version4;

import java.util.*;

public class Client {

public static void main (String[] args) {
    System.out.println ("this is a bill database");
    System.out.println ("add a user?(Y/N)");

    Scanner input = new Scanner(System.in);
    String answer = input.nextLine ();
    ArrayList ary = new ArrayList ();

    for (int i=1 ; i < 100; i++) {
        if (answer.equalsIgnoreCase("y")) {
            Bill bill1 = new Bill();
            System.out.println("user first name:");
            bill1.setFname (input.nextLine());
            System.out.println("user Last name:");
            bill1.setLname (input.nextLine());
            System.out.println ("add a user?(Y/N)");
            answer = input.nextLine ();
        } else if (answer.equalsIgnoreCase ("n")) {
            if (Bill.getBillCounter () == 0) {
                System.out.println ("the Database is empty");
                break;
            } else {
                System.out.println ("Number of Users:  "
                        + Bill.getBillCounter ());
                break;
            }
        } else {
            while (!answer.equalsIgnoreCase ("n")
                    && !answer.equalsIgnoreCase ("y")) {
                System.out.println ("add a user?(Y/N)");
                answer = input.nextLine ();
                }
            }
        }
    }
}

请帮我填写此代码。

3 个答案:

答案 0 :(得分:7)

你覆盖它们是因为你在每个循环上创建一个新的Bill并且永远不会将它们保存在任何地方。我相信您想将它们添加到ArrayList

首先,您应该为ArrayList添加类型:

ArrayList<Bill> ary = new ArrayList<Bill>();

然后,在您收到用户关于是否添加新Bill的输入之前,您应该将当前的一个添加到此列表中:

...
System.out.println("user Last name:");
bill1.setLname(input.nextLine());
ary.add(bill1);
...

答案 1 :(得分:1)

您尚未使用ArrayList,需要在Bill's循环的末尾添加for个对象。

ary.add(bill1);

并向ArrayList

添加一个类型
ArrayList<Bill> ary = new ArrayList<Bill>();

答案 2 :(得分:0)

这是Bill类......

package assginment1_version2;

public class Bill {

/**
 *  Attributes of a bill
 */
private String firstName;
private String lastName;
private int paymentDeadline;
private int paymentCode;
private int billCode;

/**
* Attribute of Bill Class
*/

      private static int BillCounter=0;

/**
 *  Methods of Bill class
 * @return number of users
 */
/*public static int getBillCounter(){
    return BillCounter;
}*/


/**
 * Class Constructor
 * @param Fname is the first name of user
 * @param Lname is the last name of user
 * @param Pdeadline is the deadline of paying the bill
 * @param Pcode introduces the payment uniquely 
 * @param Bcode introduces the bill uniquely
 */
    public Bill (){
        BillCounter++;
    }

/**
 *  FirstName methods 
 *  method to set FirstName
 * @param n is the input of setname method as a user name
 */
public void setFname (String n){
    firstName=n;
}
// method to get FirstName
public String getFname (){
    return firstName;
}


/**
 *  LastName methods
 *  method to set LastName
 */
public void setLname (String m){
    lastName=m;
}
// method to get LastName 
public String getLname(){
    return lastName;
}


/**
 * PaymentDeadline methods
 * method to set PaymentDeadline     
 */
public void setPaymentDeadline(int m){
    paymentDeadline= m;
}
//method to get PaymentDeadline
public int getPaymentDeadline(){
    return paymentDeadline;
}

/*
 * PaymentCode methods
 * Method to set PaymentCode
 */
public void setPaymentCode (int m){
    paymentCode=m;
}
//method to get PaymentCode
public int getPaymentCode(){
    return paymentCode;
}

/*
 * Methods of BillCode
 * method to set BillCode 
 */
public void setBcode(int Bcode){
    billCode=Bcode;
}
//method to get BillCode
public int getBcode(){
    return billCode;
}
}