[لسلام عليكم ورحمة الله وبركاته
مطلوب ادخل بيانات المنتج اسم , رقم, سعر الى ان ادخل رقم المنتج ب -1
ثم راح احسب اعلى قيمة والمتوسط للممنتجات
بحيث تكون مخرجات بهالشكل
Example:
Enter product id: 1001
Enter product name: Pepsi
Enter product price: 1
Enter product id: 1021
Enter product name: Kit Kat
Enter product price: 2
Enter product id: 1011
Enter product name: Chips
Enter product price: 0.5
Enter product id: -1
-----------------------------
The most expensive product is:
Id: 1021 Name: Kit Kat
Price: 2 S.R.
شavrage price = 1.167
هذا الكود اللي كتبته
import java. util.*;
public class products
{
static Scanner console=new Scanner(System.in);
static final int SENTINEL=-1;
public static void main(String[]args)
{
int id,i,maxid;
String name,maxname;
double price,sum,Avarage,maxprice,temp;
sum=0;
i=0;
maxprice=0;
maxid=0;
maxname="";
System.out.print("Enter the id of product");
id=console.nextInt();
System.out.print("Enter the name of product");
name=console.next();
System.out.print("Enter the price of product");
price=console.nextDouble();
while (id != SENTINEL)
{
sum=sum+price;
i++;
System.out.print("Enter the id of product");
id=console.nextInt();
System.out.print("Enter the name of product");
name=console.next();
System.out.print("Enter the price of product");
price=console.nextDouble();
if(i!=0)
{
Avarage=sum/i;
System.out.println("Avarage="+Avarage);
}
if(price>maxprice)
{
maxprice=price;
maxname=name;
maxid=id;
}
System.out.println("The most expensive product is:="+maxprice);
System.out.println("maxname :="+maxname);
System.out.println("maxid :="+maxid);
}
}
}
والمشكلة عندي اذا دخلت بيانات منتجين يحسب لي المتوسط واعلى قيمة وهو المفروض مايبدا يحسب الااذا دخلت رقم -1 في ادخال رقم المنتج
بليز اشروحوا لي ليش واجهتني هالمشكلة ....
