Reputation: 363
I need a programm, where I can type in numbers and in the end it gives me the highest number. Why doesn't it work like that? What do I need to change?
public class Program
{
public static void Main()
{
double[] input = new double[12];
for (int i = 1; i <= 12; i++)
{
Console.Write(" Type in {0} number:", i);
input = [Convert.ToInt32(Console.ReadLine())];
}
Console.WriteLine("The highest number is {0}", input.Max(element => Math.Abs(element)));
Console.ReadKey();
}
}
Upvotes: 0
Views: 3034
Reputation: 888
As @artokai mentioned you don't need to store all entered numbers.
Try the following:
double heighest = Double.MinValue;
for (int i = 0; i < 12; i++)
{
Console.Write(" Type in {0} number:", i);
double input = (Convert.ToDouble(Console.ReadLine());
if (input > heighest)
heighest = input
}
Console.WriteLine("The highest number is {0}", highest);
Upvotes: 0
Reputation: 17402
Is the requirement to have a Double or Int? Anyways, you can just simple store the highest number each time a new number is entered by doing a simple comparison.
public static void Main()
{
var currentNumber = 0;
for (var i = 1; i <= 12; i++)
{
Console.Write(" Type in {0} number: ", i);
var number = Console.ReadLine();
int result;
if (int.TryParse(number, out result))
{
if (currentNumber < result)
{
currentNumber = result;
}
}
}
Console.WriteLine("The highest number is {0}", currentNumber);
Console.ReadKey();
}
Upvotes: 0
Reputation: 2221
You need to make it so its converting to double and also setting to each individual element
input[i] = Convert.ToDouble(Console.ReadLine());
and then change this because arrray starts at 0
for (int i = 0; i <= 11; i++)
Upvotes: 1
Reputation: 6368
As @Ashad Shanto said you must use Convert.ToDouble
and you must use input[i]
instead of input
. So your code should look like:
public class Program
{
public static void Main()
{
double[] input = new double[12];
for (int i = 0; i < 12; i++)
{
Console.Write(" Type in {0} number:", i);
input[i] = [Convert.ToDouble(Console.ReadLine())];
}
Console.WriteLine("The highest number is {0}", input.Max(element => Math.Abs(element)));
Console.ReadKey();
}
}
Upvotes: 0