ms_learn_csharp/033_Debug_console_apps_2/Debug101/Program.cs
2024-09-30 13:26:41 -03:00

42 lines
1.2 KiB
C#

string? readResult;
int startIndex = 0;
bool goodEntry = false;
int[] numbers = { 1, 2, 3, 4, 5 };
// Display the array to the console.
Console.Clear();
Console.Write("\n\rThe 'numbers' array contains: { ");
foreach (int number in numbers) {
Console.Write($"{number} ");
}
// To calculate a sum of array elements,
// prompt the user for the starting element number.
Console.WriteLine($"}}\n\r\n\rTo sum values 'n' through 5, enter a value for 'n':");
while (goodEntry == false) {
readResult = Console.ReadLine();
goodEntry = int.TryParse(readResult, out startIndex);
if (startIndex > 5) {
goodEntry = false;
Console.WriteLine("\n\rEnter an integer value between 1 and 5");
}
}
// Display the sum and then pause.
Console.WriteLine(
$"\n\rThe sum of numbers {startIndex} through " +
$"{numbers.Length} is: {SumValues(numbers, startIndex)}"
);
Console.WriteLine("press Enter to exit");
readResult = Console.ReadLine();
// This method returns the sum of elements n through 5
static int SumValues(int[] numbers, int n) {
int sum = 0;
for (int i = n; i < numbers.Length; i++) {
sum += numbers[i];
}
return sum;
}