此代码用于将这些对象打印到名为 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;
}
答案 0 :(得分:2)
更改逻辑如下:
遍历数组
for (int i = 0; i < h.length; i++){
if(h[i].balance>3000){
p.println(h[i]);
}
}