用户键入时无法显示正确的最大值 (1 2 3 4 5 6 7 8 9 10)
Having trouble displaying correct max value when user types (1 2 3 4 5 6 7 8 9 10)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var numbers = textBox1.Text.Split(' ');
var min = numbers.Min();
var max = numbers.Max();
textBox2.Text = string.Format("min: {0} max: {1}", min, max);
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
用户键入时无法显示正确的最大值 (1 2 3 4 5 6 7 8 9 10)
我是编程新手,我对将 max {0} 更改为什么以使 10 显示为 10 而不是 9 有点困惑。我想知道是否有人可以提供帮助
您有一个字符串数组,因此当您在其上使用 Min
和 Max
时,这些值将作为字符串进行比较。
虽然作为数字 10 > 9
,作为字符串 "10" < "9"
,因为作为字符串,它们将被排序为 "0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"
。
您可以拆分字符串,然后将每个字符串转换为一个数字:
int[] numbers =
textBox1.Text.Split(' ')
.Select(s => Int32.Parse(s))
.ToArray();
现在您可以对值使用 Min
和 Max
,它们将作为数字进行比较。
您从 var numbers = textBox1.Text.Split(' ');
中提取的值将是一个字符串数组,而不是数字。您需要解析 numbers
数组中的条目,将它们从字符串转换为数字。实际上,序列中的 "max" 字符串是 9,而不是 10。因为 9 排序高于 1。
string textBox1 = "1 2 3 4 5 6 7 8 9 10";
var numbersAsStrings = textBox1.Text.Split(' ');
List<double> numbersAsDoubles = new List<double>();
foreach (var numberAsString in numbersAsStrings)
{
double numberAsDouble;
if (double.TryParse(numberAsString,out numberAsDouble))
{
numbersAsDoubles.Add(numberAsDouble);
}
}
var min = numbersAsDoubles.Min();
var max = numbersAsDoubles.Max();
var outputMessage = string.Format("min: {0} max: {1}", min, max);
textBox2.Text = outputMessage;
这种方法的优点是它使用了TryParse
。如果您只使用 Parse
并且使用类型不是数字或 space 将抛出 System.FormatException
。如果您尝试 运行 这段代码
就可以看到这个
string textBox1 = "1 2 3 4 5 6 7 8 9 10 A";
var numbers = textBox1.Split(' ').Select(int.Parse).ToArray();
如果您的用户输入 10.1
也会出现同样的错误,因为您使用的是 int.Parse
而不是 double.Parse
但也许这就是您想要的。
var numbers = textBox1.Text.Split(' ')
.Select(int.Parse).ToArray();
var min = numbers.Min();
var max = numbers.Max();
在您的表单中添加第二个文本框,并将此行添加到所有内容下方。
textbox2.Text = string.Format("min: {0} max: {1}", min, max);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var numbers = textBox1.Text.Split(' ');
var min = numbers.Min();
var max = numbers.Max();
textBox2.Text = string.Format("min: {0} max: {1}", min, max);
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
用户键入时无法显示正确的最大值 (1 2 3 4 5 6 7 8 9 10) 我是编程新手,我对将 max {0} 更改为什么以使 10 显示为 10 而不是 9 有点困惑。我想知道是否有人可以提供帮助
您有一个字符串数组,因此当您在其上使用 Min
和 Max
时,这些值将作为字符串进行比较。
虽然作为数字 10 > 9
,作为字符串 "10" < "9"
,因为作为字符串,它们将被排序为 "0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"
。
您可以拆分字符串,然后将每个字符串转换为一个数字:
int[] numbers =
textBox1.Text.Split(' ')
.Select(s => Int32.Parse(s))
.ToArray();
现在您可以对值使用 Min
和 Max
,它们将作为数字进行比较。
您从 var numbers = textBox1.Text.Split(' ');
中提取的值将是一个字符串数组,而不是数字。您需要解析 numbers
数组中的条目,将它们从字符串转换为数字。实际上,序列中的 "max" 字符串是 9,而不是 10。因为 9 排序高于 1。
string textBox1 = "1 2 3 4 5 6 7 8 9 10";
var numbersAsStrings = textBox1.Text.Split(' ');
List<double> numbersAsDoubles = new List<double>();
foreach (var numberAsString in numbersAsStrings)
{
double numberAsDouble;
if (double.TryParse(numberAsString,out numberAsDouble))
{
numbersAsDoubles.Add(numberAsDouble);
}
}
var min = numbersAsDoubles.Min();
var max = numbersAsDoubles.Max();
var outputMessage = string.Format("min: {0} max: {1}", min, max);
textBox2.Text = outputMessage;
这种方法的优点是它使用了TryParse
。如果您只使用 Parse
并且使用类型不是数字或 space 将抛出 System.FormatException
。如果您尝试 运行 这段代码
string textBox1 = "1 2 3 4 5 6 7 8 9 10 A";
var numbers = textBox1.Split(' ').Select(int.Parse).ToArray();
如果您的用户输入 10.1
也会出现同样的错误,因为您使用的是 int.Parse
而不是 double.Parse
但也许这就是您想要的。
var numbers = textBox1.Text.Split(' ')
.Select(int.Parse).ToArray();
var min = numbers.Min();
var max = numbers.Max();
在您的表单中添加第二个文本框,并将此行添加到所有内容下方。
textbox2.Text = string.Format("min: {0} max: {1}", min, max);