[英]Where did these numbers come from?
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.所以我这里有一个代码,我要做的是从字符串(“1 2 3 4 5 6 66”)中获取最大值和最小值,当我尝试从该字符串创建一个 char 数组并从中获取最大值时和最小我得到 54 作为最大值和 32 作为最小值。 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看这里
https://www.asciitable.com/ https://www.asciitable.com/
You will see that the character 'c' is decimal 54你会看到字符 'c' 是十进制的 54
and that " " (space) has decimal value 32并且“”(空格)的十进制值为 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.