我如何将数字从文本框放到数组中并对它们进行一些操作 [英] how i can putting numbers from text box to an array and do some operation on thoese
本文介绍了我如何将数字从文本框放到数组中并对它们进行一些操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的朋友们:
我想从用户那里拿出10个数字,向我显示最小和最大的数字,我想用数组来做.在Windows应用程序中.请帮帮我.
谢谢,
hi my friends:
i want to take 10 numbers from user that show me the smallest and biggest number and i want to do it with array. in windows application. please help me.
thanks
推荐答案
好,我会帮您的,但是显示最大和最小值的算法必须您自己编写.
步骤:
1)创建新的Windows应用程序项目
2)在表格上:
-1个TextBox
-3个按钮(将名称更改如下:BtnAdd,BtnClear,BtnDisplayResults)
-1个ListBox
3)复制并粘贴以下代码:
OK, i''ll help you, but algorithm to display the biggest and smallest values, you must to write by yourself.
Steps to do:
1) Create new windows application project
2) On the form place:
- 1x TextBox
- 3x Buttons (change the names as follow: BtnAdd, BtnClear, BtnDisplayResults)
- 1x ListBox
3) copy and paste the code below:
Public Class Form1
Dim numbers As List(Of Integer) = Nothing 'declare list of integer
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
numbers = New List(Of Integer) 'initialize variable
' Add any initialization after the InitializeComponent() call.
End Sub
Protected Overrides Sub Finalize()
numbers = Nothing
MyBase.Finalize()
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
Dim sText As String = String.Empty
sText = Me.TextBox1.Text 'get text from TextBox
numbers.Add(Integer.TryParse(sText, 0)) 'try to add parsed value
Me.TextBox1.Text = String.Empty 'clear TextBox
Me.TextBox1.Focus()
Me.ListBox1.Items.Add(sText)
End Sub
Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
Me.ListBox1.Items.Clear()
numbers.Clear()
End Sub
Private Sub BtnDisplayResults_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDisplayResults.Click
'here you need to write custom algorithm
End Sub
End Class
4)编译项目
我使用: ListOf(T) [
4) Compile project
I use: ListOf(T)[^] to store numbers.
您所输入的代码需求在下面(您应该将值写在文本框内,并在文本框之间插入空格):
The codes that you need are below (You should write your values inside a textbox inserting spaces between them):
string[] strArray = TextBox1.Text.Split(' ');
int biggest = Int32.MinValue;
int smallest = Int32.MaxValue;
for (int i = 0; i < strArray.Length; i++)
{
int intVal = Int32.Parse(strArray[i].Trim());
if (intVal > biggest)
biggest = intVal;
if (intVal < smallest)
smallest = intVal;
}
MessageBox.Show("The biggest: " + biggest.ToString() +
", The smallest: " + smallest.ToString());
这篇关于我如何将数字从文本框放到数组中并对它们进行一些操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文