18-09-17, 01:42 AM
(آخر تعديل لهذه المشاركة : 18-09-17, 05:43 PM {2} بواسطة أبو نوره.
تعديل السبب: تصحيح خطا املائي
)
مرحبا اخى
الكود سليم بشكل كبير باستثناء خطئين:
1- لم تضع int amount للايداع deposit كما فعلت مع السحب withdraw
2- خطا املائي داخل عملية السحب withdraw في كلمة abount والصحيح amount
الكود بعد التصحيح
نتيجة التجربه
الكود سليم بشكل كبير باستثناء خطئين:
1- لم تضع int amount للايداع deposit كما فعلت مع السحب withdraw
2- خطا املائي داخل عملية السحب withdraw في كلمة abount والصحيح amount
الكود بعد التصحيح
PHP كود :
public class BankAccount {
private int balance;
public int getBalance() {
return balance;
}
void deposit(int amount) {
balance = balance + amount;
}
public int withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
return amount;
} else {
System.out.println("there is not enough money in the account to satisfy the requested withdraw");
}
return 0;
}
// Tester method
public static void main(String[] args) {
BankAccount account = new BankAccount(); // call default constructor
account.deposit(1000); // initial deposit
// Try to withdraw 300, four times in a row
//-----------------------------------------
for (int i = 0; i < 4; i += 1) {
int amount = account.withdraw(300);
System.out.println("Withdrew $" + amount + ". Now we have $"
+ account.getBalance() + " left.");
}
}
}
نتيجة التجربه
PHP كود :
Withdrew $300. Now we have $700 left.
Withdrew $300. Now we have $400 left.
Withdrew $300. Now we have $100 left.
there is not enough money in the account to satisfy the requested withdraw
Withdrew $0. Now we have $100 left.
