在使用显示数字列表的 visual basic 中使用预测试 do...循环对应用程序进行编码时遇到问题
Having trouble coding an application using a pretest do...loop in visual basic that displays a list of numbers
在我的家庭作业中,用户在表单应用程序中有两个文本框来输入数字。在发件人框中,用户将输入他们想要在列表中开始的第一个数字。在收件人框中,他们将输入他们希望在列表中显示的最后一个号码。
例如:
发件人:1
至:5
名单:
1个
2个
3个
4个
5
作业要求我使用预测试 do...loop 来完成此任务。问题是我不知道如何让代码使用用户输入的数字。
编辑:
这是我当前的界面:
interface of application
我已经编写了 For...Next 循环。我将 post 下面的代码。我现在必须完成与预测试 Do...Loop 相同的概念。我不知道如何让循环显示用户指定的数字范围。
代码...下一个:
Private Sub btnForNext_Click(sender As Object, e As EventArgs) Handles btnForNext.Click
' Display a list of numbers.
Dim intFrom As Integer
Dim intTo As Integer
Integer.TryParse(txtFrom.Text, intFrom)
Integer.TryParse(txtTo.Text, intTo)
lstNumbers.Items.Clear()
For intList As Integer = intFrom To intTo
lstNumbers.Items.Add(intList)
Next intList
End Sub
我试过使用 Dim intList as Integer = intFrom To intTo
但这给了我预期的语句结束错误。
我试过你的代码,它按预期工作,没有任何错误。
这只是我的想法,但从事件处理程序声明来看,我看到它处理按钮 "btnForNext" 的点击事件,看看你的界面,可能不是你应该为 "Do...Loop Pretest" 点击的按钮.
也许您刚刚为错误的按钮编写了事件代码。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'You get the input from the user exactly like you did for the For loop
Dim intFrom As Integer
Dim intTo As Integer
Integer.TryParse(TextBox1.Text, intFrom)
Integer.TryParse(TextBox2.Text, intTo)
ListBox2.Items.Clear()
'You want your do loop to keep looping While the intFrom is less than or Equal to intTo
'The trick is to increment intFrom on every iteration or you will have an infinite loop
Do While intFrom <= intTo
ListBox2.Items.Add(intFrom)
intFrom += 1
Loop
'Alternatively
'Here you loop will continue Until intFrom is greater than intTo
Do Until intFrom > intTo
ListBox2.Items.Add(intFrom)
intFrom += 1
Loop
'Both are pre-tests - choose one
End Sub
在我的家庭作业中,用户在表单应用程序中有两个文本框来输入数字。在发件人框中,用户将输入他们想要在列表中开始的第一个数字。在收件人框中,他们将输入他们希望在列表中显示的最后一个号码。
例如:
发件人:1 至:5
名单: 1个 2个 3个 4个 5
作业要求我使用预测试 do...loop 来完成此任务。问题是我不知道如何让代码使用用户输入的数字。
编辑:
这是我当前的界面:
interface of application
我已经编写了 For...Next 循环。我将 post 下面的代码。我现在必须完成与预测试 Do...Loop 相同的概念。我不知道如何让循环显示用户指定的数字范围。
代码...下一个:
Private Sub btnForNext_Click(sender As Object, e As EventArgs) Handles btnForNext.Click
' Display a list of numbers.
Dim intFrom As Integer
Dim intTo As Integer
Integer.TryParse(txtFrom.Text, intFrom)
Integer.TryParse(txtTo.Text, intTo)
lstNumbers.Items.Clear()
For intList As Integer = intFrom To intTo
lstNumbers.Items.Add(intList)
Next intList
End Sub
我试过使用 Dim intList as Integer = intFrom To intTo
但这给了我预期的语句结束错误。
我试过你的代码,它按预期工作,没有任何错误。 这只是我的想法,但从事件处理程序声明来看,我看到它处理按钮 "btnForNext" 的点击事件,看看你的界面,可能不是你应该为 "Do...Loop Pretest" 点击的按钮. 也许您刚刚为错误的按钮编写了事件代码。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'You get the input from the user exactly like you did for the For loop
Dim intFrom As Integer
Dim intTo As Integer
Integer.TryParse(TextBox1.Text, intFrom)
Integer.TryParse(TextBox2.Text, intTo)
ListBox2.Items.Clear()
'You want your do loop to keep looping While the intFrom is less than or Equal to intTo
'The trick is to increment intFrom on every iteration or you will have an infinite loop
Do While intFrom <= intTo
ListBox2.Items.Add(intFrom)
intFrom += 1
Loop
'Alternatively
'Here you loop will continue Until intFrom is greater than intTo
Do Until intFrom > intTo
ListBox2.Items.Add(intFrom)
intFrom += 1
Loop
'Both are pre-tests - choose one
End Sub