Created
May 22, 2024 22:48
-
-
Save Retrockit/bb111e81e58b9b9ebc30330a077decc0 to your computer and use it in GitHub Desktop.
Array Homework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Hello and welcome to the Array Homework program. This program is designed to demonstrate the use of arrays in C#. | |
// The program will create an array of 3 names, store them in an array variable | |
// prompt the user for a number to select a name from the array | |
// display the name selected by the user | |
// and check for invalid numbers entered by the user. | |
// The program will also demonstrate the use of a do while loop to display the names in the array. | |
string[] names = new string[3] { "John", "Jane", "Joe" }; | |
int nameIndex; | |
Console.WriteLine("Hello and welcome to the Array Homework program."); | |
Console.WriteLine("This program is designed to demonstrate the use of arrays in C#."); | |
Console.WriteLine("The program will create an array of 3 names, store them in an array variable"); | |
Console.WriteLine("prompt the user for a number to select a name from the array"); | |
Console.WriteLine("display the name selected by the user"); | |
Console.WriteLine("and check for invalid numbers entered by the user."); | |
Console.WriteLine("The program will also demonstrate the use of a do-while loop to display the names in the array."); | |
Console.WriteLine("\nPlease enter a number between 1 and 3 to select a name from the array:"); | |
// Get the user input | |
string userInput = Console.ReadLine(); | |
Console.WriteLine(""); | |
// Check if the user input is a valid number using the TryParse method in a do while loop | |
do | |
{ | |
if (int.TryParse(userInput, out nameIndex) && nameIndex >= 1 && nameIndex <= 3) | |
{ | |
Console.WriteLine($"The name at index {nameIndex} is {names[nameIndex - 1]}."); | |
break; | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input. Please enter a number between 1 and 3:"); | |
userInput = Console.ReadLine(); | |
} | |
} while (true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment