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

مساعده في الكود

بدأه *en* في 24 أكتوبر 2010 · 5 رد · 469 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
import java.util.Scanner;

/*

 Q2) A company sells 6 products whose prices are as follow: P1, $10.00; P2, $5.00; P3, $2.25; P4,
$15.65; P5, $42.75; and P6, $24.35. Write an application the reads a series of pairs as follows:
a) Product Number
b) Quantity Sold
The application should use a switch statement to determine the price for each product. It should
calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to
determine when the application should stop the loop and display the final results.

 */
public class company {

	public static void main (String[] args){

		 double qs ;

		int pn;

		double totalqs = 0;
	    int totalpn = 0;

		Scanner input = new Scanner (System.in);

		pn = input.nextInt();

		while (pn != -1){

			 switch (pn){

			 case 1:
				 qs = input.nextDouble();
				 totalqs += qs; 

				 break;

			 case 2:
				 qs = input.nextDouble();
				 totalqs += qs; 


				 break;

			 case 3: 
				 qs = input.nextDouble();
				 totalqs += qs; 

				 break;

			 case 4: 
				 qs = input.nextDouble();
				 totalqs += qs; 

				 break;

			 case 5: 	 
				 qs = input.nextDouble();
				 totalqs += qs; 

				 break;

			 case 6: 

				 qs = input.nextDouble();
				 totalqs += qs;

				 break;

			 } 



			totalpn += pn;
			pn++;



		}






		    System.out.println("the total of productNumber :");
			System.out.println(totalpn);

			System.out.printf("the total of  Quantity Sold : %.2f ",totalqs);

		}
	}



		/*
1
10.00
2
5.00
3
2.25
4
15.65
5
42.75
6
24the total of productNumber :
-2147483647
the total of  Quantity Sold : 22.25 

*/

مايشتغل الكوووود عدل يعطيني قيم موعدله !!؟؟

#2

حاولى فى هدا الكود

public class company {

    public static void main(String[] args) {
        double qs;
        int pn = 0;
        double totalqs = 0;
        int totalpn = 0;
        Scanner input = new Scanner(System.in);
        while (pn != -1) {
            System.out.print("pn: ");
            pn = input.nextInt();
            if (pn != -1) {
                System.out.print("Qs : ");
                switch (pn) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                        qs = input.nextDouble();
                        totalqs += qs;
                        break;
                }
                totalpn += pn;
                pn++;
            }
        }
        System.out.println("the total of productNumber :");
        System.out.println(totalpn);
        System.out.printf("the total of  Quantity Sold : %.2f %n", totalqs);
    }
}

وما النتائج المتوقعه لتاكيد عمل البرنامج

#3

yes...

ثاااااااااااانكس على المساااااعده

يزااااااك الله خير ماقصرت صج انك جافا2010 بلامنااازع

:)

هذا الناتج

pn: 1

Qs : 10.00

pn: 2

Qs : 5.00

pn: 3

Qs : 2.25

pn: 4

Qs : 15.65

pn: 5

Qs : 42.75

pn: 6

Qs : 24.35

pn: -1

the total of productNumber :

21

the total of Quantity Sold : 100.00

#4

ماشاء الله طريقتك وايد منظمه

شكلي بعتمد هطريقه في التفكير

^^

#5

لقد أرفقت إليك الحل لكن ليس كاملا... لقد تركت لك هذه الجمله ك assignment ... :D

اقتباس
Use a sentinel-controlled loop to determine when the application should stop the loop and display the final results.

فكري فيها و ستجديها سهله بإذن الله ...

class company
{

	public static void main(String[] args)
	{
		int p_number, quantity = 0;
		double part_prices[] = new double[] { 10.0, 5.0, 2.25, 15.65, 42.75,
				24.35 };
		int quantities[] = new int[6];
		Scanner stdin = new Scanner(System.in);

		out("Enter Product #, (from 1 to 6, -1 to exit):\n");
		p_number = stdin.nextInt();
		out("Enter quantity: \n");
		quantity = stdin.nextInt();

		switch (p_number)
		{
		case 1:
			quantities[0] += quantity;
			break;
		case 2:
			quantities[1] += quantity;
			break;
		case 3:
			quantities[2] += quantity;
			break;
		case 4:
			quantities[3] += quantity;
			break;
		case 5:
			quantities[4] += quantity;
			break;
		case 6:
			quantities[5] += quantity;
			break;
		default:
			out("Invalid choice!\n");
		}

		out("Total retail value: \n");
		for (int i = 0; i < part_prices.length; i++)
		{
			out("For part #" + (i + 1) + " with price " + part_prices
					+ " the user buys " + quantities + "quantities,  with total price= "
					+ (quantities * part_prices));
		}

	}

	private static void out(Object o)
	{
		System.out.println(o);
	}
}
#6

أخي الكريم احنا ماخذينا arrays عشان جذي مو مسموح نحل فيها

بس شكلها أسهل وااايد من الكود الطووويل

راح أرجع لها إن شاء الله لمن ناخذ الarrays وأحل الواجب بس هااا لازم تعطيني درجات .. :)

جزاك الله خير على المجهود الطيب أخي الكريم

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