Reseting max and min values C#
I am putting together a program that takes 20 random numbers, sorts them into 4 sections and calculates the min and max values for each section. The issue I'm having is getting the min and max values to reset to default after each run through.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace 3
{
class Program
{
static void Main(string[] args)
{
string ln;
string line;
int value = 0;
int max = 0;
int min = 100;
using (TextReader tr = new StreamReader("values.txt"))
{
string currentLine = null;
do
{
for (int i = 1; i < 6; i++)
{
currentLine = tr.ReadLine();
if (currentLine == null)
{
break;
}
Console.WriteLine(currentLine);
value = int.Parse(currentLine);
if (value > max)
max=value;
if (i % 5 == 0)
{
Console.WriteLine("the Max is " + max);
Console.WriteLine("The Min is " + min);
Console.WriteLine("-----");
}
}
} while (currentLine != null);
Console.ReadLine();
}
}
}
}
The error with my code is that I have no way of handling the max values once the highest value ,or m开发者_如何学编程in values, have been processed whether it be at the beginning of the file or in the middle.
You can reset max and min "after they are used" - inside the if block, after the printing. Use assignment statements.
Let's take a simple example, say 8 numbers divided into 2 sections.
For the numbers 9, 2, 5, 4, 7, 1, 2, 8:
9, 2, 5, 4 is in section 1 and 7, 1, 2, 8 is in section 2.
While working with section 1, you get max = 9 and min = 2. What your code is doing is continue to work with section 2 without reseting max and min to appropriate values. You would always end up with 9 for max.
What you should do is as @David-B suggested; You should reset the max and min value just like the way you have done it at the beginning. The right place to do it is right after you have used the min and max value.
精彩评论