[英]How I can add input user value to table in C#? To return max and min value?
using System;
using System.Data;
namespace ConsoleMinMax
{
class MinMaxValue
{
static void Main(String[] args)
{
int[] tableWithValue = { 0, 0, 0, 0, 0 }; //This is a wrong way
int i = 0;
for (; i < 5; i++)
{
Console.WriteLine("Get number");
int number = Convert.ToInt32(Console.ReadLine());
tableWithValue[i] += number;
}
Console.WriteLine("Min value: " + tableWithValue.Min());
Console.WriteLine("Max value: " + tableWithValue.Max());
}
}
}
I tried made a new table with user input to check max and min value.我尝试用用户输入创建一个新表来检查最大值和最小值。 But I haven't idea how I can add user input value to table.但我不知道如何将用户输入值添加到表中。 I thought that value may add method tableWithValue.Append(number) like in Python but doesn't work in C#.我认为该值可能会像在 Python 中一样添加方法 tableWithValue.Append(number) 但在 C# 中不起作用。 Please for help.请帮忙。
In order to use Linq , in your case .Min()
and .Max()
you should add using System.Linq
:为了使用Linq ,在你的情况下.Min()
和.Max()
你应该using System.Linq
添加:
using System;
using System.Data;
using System.Linq; // <- you are going to query with a help of Linq (Min, Max etc.)
...
// It should compile now
Console.WriteLine($"Min value: {tableWithValue.Min()}");
Console.WriteLine($"Max value: {tableWithValue.Max()}");
...
To brush your code slightly:稍微刷一下你的代码:
using System;
using System.Data;
using System.Linq;
namespace ConsoleMinMax
{
class MinMaxValue
{
static void Main(String[] args)
{
// Array of 5 items
int[] tableWithValue = new int[5];
// do not use magic contsants - 5 - but tableWithValue.Length
for (int i = 0; i < tableWithValue.Length; i++)
{
Console.WriteLine("Get number");
//TODO: Better int.TryParse to validate user input
int number = Convert.ToInt32(Console.ReadLine());
// why +=? Just assign =
tableWithValue[i] = number;
}
// Time to simple Linq
Console.WriteLine($"Min value: {tableWithValue.Min()}");
Console.WriteLine($"Max value: {tableWithValue.Max()}");
}
}
}
You are using array and array do not have "add" method (they can be concatenated with other array).您正在使用数组并且数组没有“添加”方法(它们可以与其他数组连接)。 For what you need to use list, like:对于您需要使用的列表,例如:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleMinMax
{
class MinMaxValue
{
static void Main(String[] args)
{
var tableWithValue = new List<int>();
for (var i = 0; i < 5; i++)
{
Console.WriteLine("Get number");
int number = Convert.ToInt32(Console.ReadLine());
tableWithValue.Add(number);
}
Console.WriteLine("Min value: " + tableWithValue.Min());
Console.WriteLine("Max value: " + tableWithValue.Max());
}
}
}
Gentlemen, please, use LINQ consistently and idiomatically:先生们,请始终如一地、地道地使用 LINQ:
var tableWithValue = Enumerable.Range(0, 5)
.Select(i =>
{
Console.Write("Get number: ");
return int.Parse(Console.ReadLine());
})
.ToList();
Console.WriteLine("Min value: " + tableWithValue.Min());
Console.WriteLine("Max value: " + tableWithValue.Max());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.