如何找到10个不同数字中的最高数字? [英] How to find the highest number among 10 different numbers?

查看:54
本文介绍了如何找到10个不同数字中的最高数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有10个文本框和1个按钮。用户将在每个文本框中输入1个数字,然后单击按钮以查找最高数字。



如何在10个数字中找到最高数字?



这是代码,但它没有用。有什么不同的方式吗?



  private   void  button1_Click( object  sender,EventArgs e)
{
if (textBox1.Text == || textBox2.Text == < span class =code-string> || textBox3.Text == || textBox4.Text == || textBox5.Text == || textBox6.Text == || textBox7.Text == < span class =code-string> || textBox8.Text == || tex tBox9.Text == || textBox10.Text ==
{
MessageBox.Show(< span class =code-string> 请输入一个数字......!);
}
else
{
int [] d = new int [groupBox1.Controls.Count];
for int i = 0 ; i < groupBox1.Controls.Count; i ++)
{
if (groupBox1.Controls [i] TextBox)
{
TextBox t = groupBox1.Controls [i];
d [i] = int .Parse(t.Text);
}
}
Array.Sort(d);
Array.Reverse(d);

}
}







问候



Haluk

解决方案

我同意Christian的观点,但我无法抗拒张贴更优雅解决方案比其他解决方案。



  int  max =(来自 c    this  .Controls.Cast< Control> ()
其中 c TextBox&& Regex.IsMatch(c.Text。 Trim(), ^ [0-9] {1,10}



选择 int .Parse(c.Text.Trim( )))。Max();


好的,所以有三个人为你完成了你的功课,所以我觉得我可以权衡。说某些不起作用,是没用的。特别是当它抛出异常时,我们怎么知道呢?异常意味着您的代码无法运行。它告诉你代码中特别错误的东西。你的总体想法会起作用(虽然它不是最优的)。那么,你真正想知道的是我该如何解决这个异常?对于出了什么问题,例外非常明确。您使用''是'来确定控件是一个文本框,然后您作为控件访问它。您需要使用as as将其强制转换为文本框,然后访问文本框中的文本。总的来说,这段代码非常复杂,并且对于不知道异常是什么的人使用了一些复杂的主题。你可以在这里和那里学习,而不是学习基础知识,而不是将代码拼凑在一起,你应该买一本书并通过它来学习,所以你先学习基础知识,不要错过成为程序员需要知道的主要部分。

Hello guys,

There are 10 textboxes and 1 button. The user is going to enter 1 number into each textbox and then click the button to find the highest number.

How can I find the highes number among 10 numbers?

This is the code but it didn''t work. Is there any different way?

private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "")
        {
            MessageBox.Show("Please enter a number...!");
        }
        else
        {
            int[] d = new int[groupBox1.Controls.Count];
            for (int i = 0; i < groupBox1.Controls.Count; i++)
            {
                if (groupBox1.Controls[i] is TextBox)
                {
                    TextBox t = groupBox1.Controls[i];
                    d[i] = int.Parse(t.Text);
                }
            }
            Array.Sort(d);
            Array.Reverse(d);

        }
    }




Regards

Haluk

解决方案

I agree with Christian, but I couldn''t resist posting a more elegant solution than the others.

int max = (from c in this.Controls.Cast<Control>()
           where c is TextBox && Regex.IsMatch(c.Text.Trim(),"^[0-9]{1,10}


") select int.Parse(c.Text.Trim())).Max();


OK, so three people have done your homework for you, so I feel I can weigh in. Saying something ''didn''t work'', is useless. Especially when it threw an exception, how were we supposed to know that ? An exception means that your code doesn''t run. It is telling you something specifically is wrong in your code. Your general idea would work ( although it''s not optimal ). So, what you really wanted to know was ''how do I fix this exception'' ? And the exception was very clear as to what was wrong. You used ''is'' to work out that the control was a textbox, but then you accessed it, as a control. You need to use ''as'' to cast it to a textbox, and then access the text in the textbox. Overall, this code was very convoluted, and used some complex topics for someone who doesn''t know what an exception is. Instead of piecing code together from snippets you learn here and there, without learning basics, you should buy a book and work through it, so you learn basics first and don''t miss major parts of what you need to know to be a programmer


这篇关于如何找到10个不同数字中的最高数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆