اذهب إلى المحتوى
  • 0

مساعدة في لغة #c

نديم يافاوي

السؤال

مرحبا اعزائي المبرمجين

كيف يمكن كتابة هكذا برنامج؟

You should create a program where the user can input 10 numbers. The speech should be saved in an array / list and after the input the user should be able to see the largest number or the smallest number or the average or all the numbers. A program run may look like the following:

Enter number 1: 12
Enter number 2: 8
Enter number 3: 34
Enter number 4: 23
Enter number 5: 9
Enter number 6: 62
Enter number 7: 41
Enter number 8: 7
Enter number 9: 14
Enter number 10: 53

Choose what you want to do
1. Show the largest number
2. Show the smallest number
3. Show the mean to one decimal
4. Display all input numbers
5. Finish this program
 

تم التعديل في بواسطة نديم يافاوي
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

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

مرفق طريقتين للحل واحدة باستخدام المصفوفات Arrays وواحدة باستخدام Lists

طريقة المصفوفة

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new int[10];
            int option = 0;
            for (int i = 0; i < 10; i++)
            {

                Console.Write("Enter number " + (i + 1) + ": ");
                numbers[i] = int.Parse(Console.ReadLine());
            }
            do
            {
                Console.WriteLine();
                Console.WriteLine("Choose what you want to do:");
                Console.WriteLine(" 1. Show the largest number");
                Console.WriteLine(" 2. Show the smallest number");
                Console.WriteLine(" 3. Show the mean to one decimal");
                Console.WriteLine(" 4. Display all input numbers");
                Console.WriteLine(" 5. Finish this program");
                option = int.Parse(Console.ReadLine());
                if (option == 1)
                {
                    int max = numbers[0];
                    for (int i = 0; i < 10; i++)
                    {
                        if (numbers[i] > max)
                            max = numbers[i];
                    }
                    Console.WriteLine("Max = " + max);
                }
                else if (option == 2)
                {
                    int min = numbers[0];
                    for (int i = 0; i < 10; i++)
                    {
                        if (numbers[i] < min)
                            min = numbers[i];
                    }
                    Console.WriteLine("Min = " + min);
                }
                else if (option == 3)
                {
                    int sum = 0;
                    int count = 10;
                    double average;
                    for (int i = 0; i < 10; i++)
                    {
                        sum += numbers[i];
                    }
                    average = sum / count;
                    Console.WriteLine("Average = " + average);
                }
                else if (option == 4)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Console.WriteLine(numbers[i]);
                    }
                }

            } while (option != 5);        
        }
    }
}

طريقة List

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int>();
            int option = 0;
            for (int i = 0; i < 10; i++)
            {
                Console.Write("Enter number " + (i + 1) + ": ");
                numbers.Add(int.Parse(Console.ReadLine()));
            }
            do
            {
                Console.WriteLine();
                Console.WriteLine("Choose what you want to do:");
                Console.WriteLine(" 1. Show the largest number");
                Console.WriteLine(" 2. Show the smallest number");
                Console.WriteLine(" 3. Show the mean to one decimal");
                Console.WriteLine(" 4. Display all input numbers");
                Console.WriteLine(" 5. Finish this program");
                option = int.Parse(Console.ReadLine());
                if (option == 1)
                {
                    Console.WriteLine("Max = " + numbers.Max());
                }
                else if (option == 2)
                {
                    Console.WriteLine("Min = " + numbers.Min());
                }
                else if (option == 3)
                {
                    Console.WriteLine("Average = " + numbers.Average());
                }
                else if (option == 4)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Console.WriteLine(numbers[i]);
                    }
                }

            } while (option != 5);
        }
    }
}

بالتوفيق،،،

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

بالإمكان عمله بطريقة ابسط وب loop واحد فقط.  وايضاً انصح بإستخدام switch في حالة تعدد الاختيارات كما هو بسؤالك. سيكون افضل. 

List<int> inputList = new List<int>();
decimal total = 0.0M;
int userChoice = 0;

for (int x = 0; x < 10; x++)
{
  Console.Write("Enter number " + (x + 1) + " :");
  inputList.Add(Convert.ToInt32(Console.ReadLine()));
  total += inputList[x];
}
Console.WriteLine();
Console.WriteLine("Choose what you want to do:");
Console.WriteLine(" 1. Show the largest number");
Console.WriteLine(" 2. Show the smallest number");
Console.WriteLine(" 3. Show the mean to one decimal");
Console.WriteLine(" 4. Display all input numbers");
Console.WriteLine(" 5. Finish this program");

userChoice = Convert.ToInt32(Console.ReadLine());

switch (userChoice)
{
  case 1: // 1. Show the largest number
    Console.WriteLine("Max : \t" + inputList.Max());
    break;
  case 2: // 2. Show the smallest number
    Console.WriteLine("Min : \t" + inputList.Min());
    break;
  case 3: // 3. Show the mean to one decimal
    Console.WriteLine("Mean : \t" + (total / inputList.Count));
    break;
  case 4: // 4. Display all input numbers
    string allNumbers = string.Join(Environment.NewLine, inputList);
    Console.WriteLine(allNumbers);
    break;
  case 5: // 5. Finish this program
    Environment.Exit(0);
    break;
}

// Keep The console open
Console.WriteLine();
Console.WriteLine("Press any key to exit ...");
Console.ReadLine();

 

تم التعديل في بواسطة محمد المري2
رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 17 ساعات قال محمد المري2:

بالإمكان عمله بطريقة ابسط وب loop واحد فقط.  وايضاً انصح بإستخدام switch في حالة تعدد الاختيارات كما هو بسؤالك. سيكون افضل. 


List<int> inputList = new List<int>();
decimal total = 0.0M;
int userChoice = 0;

for (int x = 0; x < 10; x++)
{
  Console.Write("Enter number " + (x + 1) + " :");
  inputList.Add(Convert.ToInt32(Console.ReadLine()));
  total += inputList[x];
}
Console.WriteLine();
Console.WriteLine("Choose what you want to do:");
Console.WriteLine(" 1. Show the largest number");
Console.WriteLine(" 2. Show the smallest number");
Console.WriteLine(" 3. Show the mean to one decimal");
Console.WriteLine(" 4. Display all input numbers");
Console.WriteLine(" 5. Finish this program");

userChoice = Convert.ToInt32(Console.ReadLine());

switch (userChoice)
{
  case 1: // 1. Show the largest number
    Console.WriteLine("Max : \t" + inputList.Max());
    break;
  case 2: // 2. Show the smallest number
    Console.WriteLine("Min : \t" + inputList.Min());
    break;
  case 3: // 3. Show the mean to one decimal
    Console.WriteLine("Mean : \t" + (total / inputList.Count));
    break;
  case 4: // 4. Display all input numbers
    string allNumbers = string.Join(Environment.NewLine, inputList);
    Console.WriteLine(allNumbers);
    break;
  case 5: // 5. Finish this program
    Environment.Exit(0);
    break;
}

// Keep The console open
Console.WriteLine();
Console.WriteLine("Press any key to exit ...");
Console.ReadLine();

 

بتاريخ 17 ساعات قال محمد المري2:

بالإمكان عمله بطريقة ابسط وب loop واحد فقط.  وايضاً انصح بإستخدام switch في حالة تعدد الاختيارات كما هو بسؤالك. سيكون افضل. 


List<int> inputList = new List<int>();
decimal total = 0.0M;
int userChoice = 0;

for (int x = 0; x < 10; x++)
{
  Console.Write("Enter number " + (x + 1) + " :");
  inputList.Add(Convert.ToInt32(Console.ReadLine()));
  total += inputList[x];
}
Console.WriteLine();
Console.WriteLine("Choose what you want to do:");
Console.WriteLine(" 1. Show the largest number");
Console.WriteLine(" 2. Show the smallest number");
Console.WriteLine(" 3. Show the mean to one decimal");
Console.WriteLine(" 4. Display all input numbers");
Console.WriteLine(" 5. Finish this program");

userChoice = Convert.ToInt32(Console.ReadLine());

switch (userChoice)
{
  case 1: // 1. Show the largest number
    Console.WriteLine("Max : \t" + inputList.Max());
    break;
  case 2: // 2. Show the smallest number
    Console.WriteLine("Min : \t" + inputList.Min());
    break;
  case 3: // 3. Show the mean to one decimal
    Console.WriteLine("Mean : \t" + (total / inputList.Count));
    break;
  case 4: // 4. Display all input numbers
    string allNumbers = string.Join(Environment.NewLine, inputList);
    Console.WriteLine(allNumbers);
    break;
  case 5: // 5. Finish this program
    Environment.Exit(0);
    break;
}

// Keep The console open
Console.WriteLine();
Console.WriteLine("Press any key to exit ...");
Console.ReadLine();

 

 

جزيل الشكر اخي محمد :)

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...