تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
توضيح الخطأ في الكود والغرض منه
#1
السلام عليكم ورحمة الله وبركاته

آمل التكرم بتوضيح الكود التالي وما هو الخطأ في الكود


/* Exercise: complete this class.
The class should have one private variable of type int for the balance.

The class should have three public methods.

getBalance(): returns the current balance.

deposit(): has one parameter of type int which is the amount being deposited.
The method should add this amount to the balance.

withdraw(): has one parameter of type int which is the amount being withdrawn.
The should update the balance and return the amount being withdrawn.  If there
is not enough money in the account to allow the withdraw, the method should
print out a message saying that there is not enough money in the account to
satisfy the requested withdraw and it should return 0 to indicate that no money
was withdrawn.

Note: the signature line of each method needs to be completed/corrected.
*/ 
public class BankAccount {
   private int balance;

 
   public int getBalance() {
return balance;
}
   
   void deposit() {
balance = balance + amount;
}
   

   public int withdraw( int amount ) {

     if (balance >= amount){
       balance -= amount;
       return abount;
}
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.");
      }
   }
}
الرد }}}
تم الشكر بواسطة:
#2
هذا سؤال تعليمي
هل تريد منا حل واجبك المدرسي؟
مع ان الشرح موجود امامك

الرد }}}
تم الشكر بواسطة: aftfm , aftfm
#3
مرحبا اخى

الكود سليم بشكل كبير باستثناء خطئين:
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 04+= 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
الرد }}}
تم الشكر بواسطة: Amir_Alzubidy , aftfm


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  هل من مساعدة فى تنفيذ هذا الكود kiki 4 2,301 06-04-21, 05:34 AM
آخر رد: kiki
  مهم جدا من يعرف يحلل هذا الكود وماعمله nablion 0 2,295 11-12-12, 10:25 PM
آخر رد: nablion

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم