So I have a code here and what I am tring to do is to get max and min value from string("1 2 3 4 5 6 66") and when I tried to make a char array from this string and get from it max and min I am getting 54 as a max and 32 as a min. HOW?
static void Main(string[] args)
{
HighAndLow("1 2 3 4 5 6 66");
}
public static string HighAndLow(string numbers)
{
char[] liczby = numbers.ToArray();
int max = liczby.Max();
int min = liczby.Min();
Console.WriteLine($"{max} {min}");
return $"{max} {min}";
}
Look here
You will see that the character 'c' is decimal 54
and that " " (space) has decimal value 32
You're getting the char codes, not the values.
Change
char[] liczby = numbers.ToArray();
to something like
char[] temp = numbers.Split(' ');
int[] liczby = temp.Select(c => int.parse(c)).ToArray();
using System;
using System.Collections.Generic;
using System.Linq;
namespace stack
{
internal class Program
{
static void Main(string[] args)
{
HighAndLow("1 2 3 4 5 6 66");
}
static string HighAndLow(string numbers)
{
if (numbers.Length > 0)
{
var listnumbers = numbers.Split(' ');
var max = int.MinValue;
var min = int.MaxValue;
foreach (var number in listnumbers)
{
var ent = int.Parse(number);
max = ent <= max ? max : ent;
min = ent >= min ? min : ent;
}
Console.WriteLine("max :"+max+" min: "+min);
return $"{max} {min}";
}
return "empty string";
}
}
}
or just replace:
char[] liczby = numbers.ToArray();
with:
var temp = numbers.Split(' ');
int[] liczby = temp.Select(c => int.Parse(c)).ToArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.