locked
Code Help RRS feed

  • Question

  • Hello,

    I was wondering how I would easily repeat the statement asking for the x & y coordinates 5 times (because the user can input up to 5 sets of coordinates (Because there are 5 suppliers)), instead of coping and pasting and having 10 variables.  

    Thanks in advance. 

    Code

    using System;

    namespace Program_2
    {
        class Program
        {
            static void Main(string[] args)
            {
                double x, y, z;
                Console.WriteLine("Enter the number of suppliers:");
                z = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter the X-Coordinate for supplier 1:");
                x = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter the y-Coordinate for supplier 1:");
                y = Convert.ToDouble(Console.ReadLine());
            }
        }
    }


    Tuesday, April 7, 2020 7:50 PM

Answers

  • here are the basics

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Item> items = new List<Item>();
    
                for (int index = 0; index < 5; index++)
                {
                    var item = new Item();
                    Console.WriteLine(" Enter the number of suppliers:");
                    item.z = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine($"Enter the X-Coordinate for supplier {index + 1}:");
                    item.x = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine($"Enter the y-Coordinate for supplier {index +1}:");
                    item.y = Convert.ToDouble(Console.ReadLine());
    
                    items.Add(item);
                    Console.Clear();
                }
    
                Console.ReadLine();
            }
        }
    
        class Item
        {
            public double x { get; set; }
            public double y { get; set; }
            public double z { get; set; }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Naomi N Tuesday, April 7, 2020 9:25 PM
    • Marked as answer by Extra Account Thursday, April 9, 2020 4:00 PM
    Tuesday, April 7, 2020 8:34 PM