[英]how to make a condition for balance to print it
This code is to print these objects to file named customer.txt . 此代码用于将这些对象打印到名为customer.txt的文件中。 It is working correctly but I am trying to put condition like if balance>3000 print that object.
它工作正常,但我试图把条件,如果平衡> 3000打印该对象。
How can I declare balance? 我该如何申报余额? Should I read the bytes for balance and print it ?
我应该读取余额的字节并打印出来吗?
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;
}
Change your logic as below: 改变你的逻辑如下:
Loop through over the array 循环遍历数组
for (int i = 0; i < h.length; i++){
if(h[i].balance>3000){
p.println(h[i]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.