Ejercicio
Arraylist De Puntos
Objectivo
Cree una estructura "Point3D", para representar un punto en el espacio 3-D, con coordenadas X, Y y Z.
Cree un programa con un menú, en el que el usuario pueda elegir:
- Agregar datos para un punto
- Mostrar todos los puntos ingresados
- Salir del programa
DEBE usar ArrayList, en lugar de matrices.
Ejemplo Ejercicio C#
Mostrar Código C#
// Import necessary namespaces for using ArrayList and other basic operations
using System; // Basic namespace for console input/output and fundamental operations
using System.Collections; // For using ArrayList
// Define the structure for representing a point in 3D space
struct Point3D
{
// Properties to store the X, Y, and Z coordinates of the point
public double X; // X-coordinate
public double Y; // Y-coordinate
public double Z; // Z-coordinate
// Constructor to initialize the coordinates of the point
public Point3D(double x, double y, double z)
{
X = x; // Set X-coordinate
Y = y; // Set Y-coordinate
Z = z; // Set Z-coordinate
}
// Method to display the point in a readable format
public void DisplayPoint()
{
// Display the point's coordinates as (X, Y, Z)
Console.WriteLine($"Point: ({X}, {Y}, {Z})");
}
}
class Program
{
// Main method to execute the program
static void Main(string[] args)
{
// Create an ArrayList to store points
ArrayList points = new ArrayList(); // ArrayList to hold Point3D objects
string choice; // Variable to store user's menu choice
// Display the program menu
do
{
// Display menu options to the user
Console.Clear(); // Clear the console for a fresh display
Console.WriteLine("3D Point Manager");
Console.WriteLine("1. Add a point");
Console.WriteLine("2. Display all points");
Console.WriteLine("3. Exit");
Console.Write("Enter your choice: ");
choice = Console.ReadLine(); // Get the user's menu choice
// Perform actions based on the user's choice
switch (choice)
{
case "1":
// Add a new point
Console.Write("Enter X coordinate: ");
double x = Convert.ToDouble(Console.ReadLine()); // Read X-coordinate
Console.Write("Enter Y coordinate: ");
double y = Convert.ToDouble(Console.ReadLine()); // Read Y-coordinate
Console.Write("Enter Z coordinate: ");
double z = Convert.ToDouble(Console.ReadLine()); // Read Z-coordinate
// Create a new Point3D object with the provided coordinates
Point3D newPoint = new Point3D(x, y, z);
// Add the point to the ArrayList
points.Add(newPoint); // Store the new point in the ArrayList
Console.WriteLine("Point added successfully.");
break;
case "2":
// Display all entered points
Console.WriteLine("Displaying all points:");
foreach (Point3D point in points) // Iterate through each point in the ArrayList
{
point.DisplayPoint(); // Call DisplayPoint method to show the point
}
break;
case "3":
// Exit the program
Console.WriteLine("Exiting the program...");
break;
default:
// Handle invalid input
Console.WriteLine("Invalid choice, please try again.");
break;
}
// Wait for the user to press a key before showing the menu again
if (choice != "3")
{
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
} while (choice != "3"); // Repeat the menu until the user chooses to exit
}
}
Salida
3D Point Manager
1. Add a point
2. Display all points
3. Exit
Enter your choice: 1
Enter X coordinate: 100
Enter Y coordinate: 100
Enter Z coordinate: 100
Point added successfully.
Código de Ejemplo Copiado!
Comparte este Ejercicio C# Sharp