0

此代码是将这些对象打印到名为customer.txt的文件中。它工作正常,但我试图设置条件,例如如果balance>3000打印该对象。

如何申报余额?我应该读取字节以获取余额并打印吗?

 File a = new File("customer.txt");

 FileWriter v = new FileWriter(a);
 BufferedWriter b = new BufferedWriter(v);
 PrintWriter p = new PrintWriter(b);


 human Iman = new human("Iman", 5000);
 human Nour = new human("Nour", 3500);
 human Redah = new human("Redah", 0);
 human iman = new human("iman", 200);
 human MohamedREDA = new human("MohamedREDA", 3000);
 human Mohamed_Redah = new human("Mohamed Redah", 2000);

 human[] h = new human[6];

 h[0] = Iman;
 h[1] = Nour;``
 h[2] = Redah;
 h[3] = iman;
 h[4] = MohamedREDA;
 h[5] = Mohamed_Redah;

 p.println(Iman);
 p.println(Nour);
 p.println(Redah);
 p.println(iman);
 p.println(MohamedREDA);
 p.println(Mohamed_Redah);

 p.flush();
 }

 }
 class human {
     public String name;
     public double balance;

     public human(String n, double b) {
         this.balance = b;
         this.name = n;

     }@
     Override
     public String toString() {
         return name + " " + balance;
     }
4

1 回答 1

2

改变你的逻辑如下:

遍历数组

for (int i = 0; i < h.length; i++){    
    if(h[i].balance>3000){
        p.println(h[i]);   
    }
}
于 2013-06-22T14:29:18.830 回答