我使用文本框从用户那里获得10个十号 [英] i have 10 ten number taken from user using textbox

查看:95
本文介绍了我使用文本框从用户那里获得10个十号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个号码来自用户使用文本框,我需要检查它是否是升序。请用代码

解决方案

转换每个到整数:

  int  i1 =  int  .Parse(myTextBox1.Text); 



然后您可以检查值:

 < span class =code-keyword> if (i1 <  i2&& i2 <  i3&& i3 <  i4)
{
...
}


逻辑简单解决方案是在迭代TextBoxes时检查升序,并将它们的Text转换为某种数字类型。有没有理由你不想这样做?



将当前TextBox的转换数字与前一个TextBox中的数字进行比较可以让你停止任何你找到一个无序的号码的时间。



当然你可以在之后用代码来构建转换后的数字数组:

  //  假设此数组包含从TextBox转换的所有数字 
int [] a1 = new int [] { 1 3 3 6 16 18 22 44 17 };

private void button1_Click(对象发​​件人,EventArgs e)
{
bool IsAscending = true ;

for int i = 0 ; i < a1.Length - 1 ; i ++)
{
Console.WriteLine(i + + a1 [i] + + a1 [i + 1 ]);

if (a1 [i] > a1 [i + 1 ])
{
IsAscending = false ;
break ;
}
}

MessageBox.Show(IsAscending.ToString());
}

如果你想获得更多,并且更多地运用你的CPU:

  //  < span class =code-comment>假设此数组包含从TextBox转换的所有数字 
int [] a1 = new int [] { 1 9 5 3 6 8 2 0 4 7 };

private void btnCheckAscendingOrder_Click(对象发​​件人,EventArgs e)
{
int [] a2 = new int [a1.Length];

Array.Copy(a1,a2,a1.Length);

Array.Sort(a2);

MessageBox.Show( 升序: +(a1 == A2)的ToString());
}


i have 4 number taken from user using textboxs and i need to check whether it is ascending order or not .please kindly suggest me with code

解决方案

Convert each to an integer:

int i1 = int.Parse(myTextBox1.Text);


You can then just check the values:

if (i1 < i2 && i2 < i3 && i3 < i4)
   {
   ...
   }

But if you are going to do a number of values, I'd suggest converting them into an array instead.


The logical simple solution would be to check for ascending order as you iterate over the TextBoxes, and convert their Text to some Type of number. Is there a reason you don't want to do that ?

Comparing the current TextBox's converted number to the number in the previous TextBox would give you the ability to stop any time you found a number out of order.

Of course you could do that in code after you have built the array of converted numbers:

// assume this array contains all the numbers converted from the TextBoxes
int[] a1 = new int[] { 1, 3, 3, 6, 16, 18, 22, 44, 17 };

private void button1_Click(object sender, EventArgs e)
{
    bool IsAscending = true;

    for (int i = 0; i < a1.Length - 1; i++)
    {
        Console.WriteLine(i + " " + a1[i] + " " + a1[i + 1]);

        if (a1[i] > a1[i + 1])
        {
            IsAscending = false;
            break;
        }
    }
     
    MessageBox.Show(IsAscending.ToString());
}

If you want to get fancier, and exercise your CPU more:

// assume this array contains all the numbers converted from the TextBoxes
int[] a1 = new int[] { 1, 9, 5, 3, 6, 8, 2, 0, 4, 7 };

private void btnCheckAscendingOrder_Click(object sender, EventArgs e)
{
    int[] a2 = new int[a1.Length];

    Array.Copy(a1, a2, a1.Length);

    Array.Sort(a2);

    MessageBox.Show("ascending order: " + (a1 == a2).ToString());
}


这篇关于我使用文本框从用户那里获得10个十号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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