“编写一个读入开始值和结束值的程序。然后程序将这两个值(包括)之间的所有偶数存储在一个数组中。然后要求用户选择一个数字(n),程序应输出第n个偶数“
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
number1 = InputBox("Enter first number")
number2 = InputBox("enter second number")
对此的任何指导都会非常感激,我完全迷失了。
答案 0 :(得分:1)
好的,编辑对我来说更有意义。获得前三个输入的正确轨道。然后我们需要为我们的输入做两件事:
1)获取用户给我们的范围内的偶数
2)如果存在,则返回第n个术语
我会像这样解决问题:
'Get our inputs
Dim number1 As Integer = CInt(InputBox("Enter first number"))
Dim number2 As Integer = CInt(InputBox("Enter second number"))
Dim nthTerm As Integer = CInt(InputBox("Enter Nth Term"))
Dim evenNumbers As New List(Of Integer)
'Now, we want to get a list of all the even numbers within n1 to n2 range
For i As Integer = number1 To number2
'if the number divided by 2 has a remainder of 0, then it's an even number
If i Mod 2 = 0 Then evenNumbers.Add(i)
Next
'Now that we have all the even #s, try to return the nth one as long as it exists
Try
'We substract 1 from the nthTerm entered by used to account for list's 0-based index
MsgBox(evenNumbers(nthTerm - 1).ToString)
Catch ex As Exception
MsgBox("Nth Term out of bounds")
End Try