الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

برنامج لحساب سعر البضائع المشتراه

بدأه smart princess في 25 ديسمبر 2009 · 11 رد · 1,844 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

اخواني كتب هذا البرنامج البسيط لادخال اسم بضاعه وثم ادخال سعرها وفي النهايه حساب السعر كامل ولكن البرنامج فقط يظهر ادخال اسم البضاعه مره واحد ف ما الحل؟؟!!

// This programe to calculate the expenditure per month
import java.util.*;
public class Expenditure{
  public static void main(String[] args){
    String name;
    double money;
    double sum=0.0;
    Scanner scan=new Scanner(System.in);
    do{
      System.out.println("Enter the goods' name");
      name=scan.nextLine();// to enter the goods' name
      System.out.println("Enter 0 to finish this loop");// to inform the user that the loop will stop whin he/she enter 0
      System.out.println("Enter the cost of this thing");
      money=scan.nextDouble();// to enter cost of that good
       System.out.println(name+ "and it's cost is" +money);// to display the good with its' cost
      sum=sum+money;//to calculate the total of all price
    }while(money !=0);
    System.out.println("The Final Total of all this things is" +sum); //to display the final price
  }
}
#2

البرنامج يقوم بطلب إدخال إسم البضاعة إلى أن يتم إدخال 0 في المتغير money

#3
import java.util.*;
public class Expenditure{
  public static void main(String[] args){
    String name;
    double money;
    double sum=0.0;
    Scanner scan=new Scanner(System.in);
    do{
        scan = new Scanner(System.in);
      System.out.print("Enter the goods' name: ");
      name = scan.nextLine();// to enter the goods' name
      System.out.println("Enter 0 to finish this loop");// to inform the user that the loop will stop whin he/she enter 0
      System.out.print("Enter the cost of " + name + ": " );
      money=scan.nextDouble();// to enter cost of that good
       System.out.println(name+ " and it's cost is " + money);// to display the good with its' cost
      sum=sum+money;//to calculate the total of all price
    }while(money !=0);
    System.out.println("The total price of all these goods is " + sum); //to display the final price
  }
}
1
#4
herch كتب:

البرنامج يقوم بطلب إدخال إسم البضاعة إلى أن يتم إدخال 0 في المتغير money

شكرا لك اخي على المرور,,,

نعم البرنامج يقوم بادخال اسم البضاعه والسعر الى ان يتم ادخال 0 في المتغير money لكن البرنامج لايسمح بادخال اسم البضاعه الا مره وحده وانا اريده كل مره ف سؤالي هو ما الخطأ؟؟

#5
I corrected it, check the code i posted
#6
NullOne كتب:

import java.util.*;
public class Expenditure{
  public static void main(String[] args){
    String name;
    double money;
    double sum=0.0;
    Scanner scan=new Scanner(System.in);
    do{
        scan = new Scanner(System.in);
      System.out.print("Enter the goods' name: ");
      name = scan.nextLine();// to enter the goods' name
      System.out.println("Enter 0 to finish this loop");// to inform the user that the loop will stop whin he/she enter 0
      System.out.print("Enter the cost of " + name + ": " );
      money=scan.nextDouble();// to enter cost of that good
       System.out.println(name+ " and it's cost is " + money);// to display the good with its' cost
      sum=sum+money;//to calculate the total of all price
    }while(money !=0);
    System.out.println("The total price of all these goods is " + sum); //to display the final price
  }
}

بوركت اخي على الرد ودائما ما شاء الله سباق في المساعده,,,

وقد لاحظت بانك ادخلت كود scan ولكن بدون متغير امامه فهل تصلح هذه الطريقة؟؟

ومتى ممكن نستخدمها؟؟

#7
/**
 * the following solution is better so please change it
 * 
 * The nextInt(), nextDouble() etc. methods don't read beyond what they have to read. 
 * lets say an int (price) has to be read and you type 20 then "Enter" the int 20
 * is read and the newline character which is caused by the "Enter" key is left in the input buffer. 
 * If you want to read next line afterwards, a new line character 
 * is seen and an empty line is returned. This looks as if nothing is read.
 * 
 * so the solution is to call next line after nextInt() to remove the new line
 * charachter from the buffer
 * 
 * Sorry for writing in english
 */

import java.util.*;
public class Expenditure{
  public static void main(String[] args){
    String name;
    double money;
    double sum=0.0;
    Scanner scan=new Scanner(System.in);
    do{
      System.out.print("Enter the goods' name: ");
      name = scan.nextLine();// to enter the goods' name
      System.out.println("Enter 0 to finish this loop");// to inform the user that the loop will stop whin he/she enter 0
      System.out.print("Enter the cost of " + name + ": " );
      money=scan.nextDouble();// to enter cost of that good
       System.out.println(name+ " and it's cost is " + money);// to display the good with its' cost
      sum=sum+money;//to calculate the total of all price
      scan.nextLine();
    }while(money !=0);
    System.out.println("The total price of all these goods is " + sum); //to display the final price
  }
}
1
#8
NullOne كتب:

/**
 * the following solution is better so please change it
 * 
 * The nextInt(), nextDouble() etc. methods don't read beyond what they have to read. 
 * lets say an int (price) has to be read and you type 20 then "Enter" the int 20
 * is read and the newline character which is caused by the "Enter" key is left in the input buffer. 
 * If you want to read next line afterwards, a new line character 
 * is seen and an empty line is returned. This looks as if nothing is read.
 * 
 * so the solution is to call next line after nextInt() to remove the new line
 * charachter from the buffer
 * 
 * Sorry for writing in english
 */

import java.util.*;
public class Expenditure{
  public static void main(String[] args){
    String name;
    double money;
    double sum=0.0;
    Scanner scan=new Scanner(System.in);
    do{
      System.out.print("Enter the goods' name: ");
      name = scan.nextLine();// to enter the goods' name
      System.out.println("Enter 0 to finish this loop");// to inform the user that the loop will stop whin he/she enter 0
      System.out.print("Enter the cost of " + name + ": " );
      money=scan.nextDouble();// to enter cost of that good
       System.out.println(name+ " and it's cost is " + money);// to display the good with its' cost
      sum=sum+money;//to calculate the total of all price
      scan.nextLine();
    }while(money !=0);
    System.out.println("The total price of all these goods is " + sum); //to display the final price
  }
}

اشكر اهتمامك اخي العزيز وبوركت عمل رائع,,,

عندي سؤال لماذا قمت بتكرار

 scan.nextLine();

في السطر الثالث قبل الاخير؟؟

#9

لقد شرحته بال-English لماذا أضفت ال-nextLine . لا استطيع الشرح بالعربي :mad: . إذا صعب عليكي فهمها اتمنى من أحد الموجودين ترجمة ال-comment

#10
NullOne كتب:

لقد شرحته بال-English لماذا أضفت ال-nextLine . لا استطيع الشرح بالعربي :mad: . إذا صعب عليكي فهمها اتمنى من أحد الموجودين ترجمة ال-comment

:blush: :blush:

خيو فهمت الcomment بس اللي اقصده هذا الكود في العاده نستخدمه للطباعه اي لادخال string وانت وضعته في الاخير ولايوجد امر طباعه قبله,,,

#11

الأخ nullone أخبرك في التعليق الأخير

أن الجملة

nextInt

تأخذ ما يتعلق بها فقط

أما enter فإنه يبقى في الStringBuffer الخاص بـ Scanner

وعندما تطلبين قراءة سطر جديد في الجملة

nextLine

فإن Scanner يتعامل مع الضغطة على الزر enter السابقة

على أساس أنها سطر جديد ويعطيك سطر فاضي ""

لهذا ينصحك باستخدام nextInt بعد كل عملية قراءة لعدد صحيح

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#12

بعد calling nextInt هناك new line character ما زالت في ال-buffer لذلك أعمل call ل nextLine حتى يصبح ال-buffer خالي من أي characters حتى تتمكني من إضافة أسماء أخرى للبضائع

مواضيع مشابهة