menu

arrow_back How do I use a void variable?

by
1 vote
My knowledge of c# is limited) Explain how, after declaring an array, to calculate the sum of the minimum and maximum values. Apparently my program is wrong, because I get the error: no conversion of void type variable to int.

static void Main(string[] args)
{
double [] Data = new double[5];
int i = 0;
while (i < 5)
{
Data[i] = double.Parse(Console.ReadLine());
Console.WriteLine();
i++;
}
int x =
Console.WriteLine(Data.Min());
int y =
Console.WriteLine(Data.Max());
Console.WriteLine(x + y);
}

5 Comments

Translate Void into Russian and I think the question goes away.
And in essence, look at the WriteLine method and what it returns, look at the Min & Max method, look at what they return.
First you need to put the result of the Min, Max method into a variable, and then output it.
LiptonOlolo , that's clear, can you help fix the program?
Thank you all, I understand that the method Data.Min returns values of type double, further action is clear, thank you all)
evans_85 , updated the commentary.
evans_85 , int x = Console.WriteLine(Data.Min());

There's no way Data.Min() gets into the X variable

1 Answer

by
 
Best answer
0 votes
I would recommend it this way. Just look at it and think about why

using System;
using System.Linq;

namespace ConsoleApp99
{
internal class Program
{
private static void Main()
{
const int size = 5;
var data = new double[size];
var i = 0;
Console.WriteLine("Enter float or double");
do
{
var parsed = double.TryParse(Console.ReadLine(), out var dp);
if (!parsed) continue;
data[i] = dp;
i++;
} while (i < size);

var x = data.Min();
Console.WriteLine($"min: {x}");
var y = data.Max();
Console.WriteLine($"max: {y}");
Console.WriteLine($"sum x + y: {x + y}");
}
}
}