For some reason, I can't understand the logic of a program that:
- Takes in a list of numbers
- Goes through them (via a for each loop) to find the least number
- displays the least number via writeline
If leastNumber must be initialized to 0, then won't the leastNumber ALWAYS be 0? (I've provided a basic txt file containing a list of various integers, that's what TwoNumbers.txt is)
string[] lines = File.ReadAllLines ( "TwoNumbers.txt" );
int leastNumber = 999;
int previousNumber = 0;
foreach (string line in lines)
{
int currentNumber = int.Parse ( line );
currentNumber = leastNumber;
if (currentNumber < previousNumber)
{
leastNumber = currentNumber;
}
}
Console.WriteLine ("The least number is: " + leastNumber);
Console.ReadLine ();