我有这个视觉基本代码来接收用户输入并打印出许多素数。例如,如果用户输入5,则输出应为:1,3,5,7,11。但我发现它有困难。这是我的代码:
Dim i, n, input, currentPrime As Integer
Dim Wrap As String = Chr(10) & Chr(13)
input = txtInput.Text
currentPrime = 1
txtAns.Text = "Prime Numbers are : " & Wrap
Do While (currentPrime <= input)
For i = currentPrime To input
For j = 2 To Fix(i / 2) + 1
If i Mod j = 0 Then
n = 1
End If
Next
If n = 1 Then
n = 0
Else
txtAns.Text = txtAns.Text & Wrap & i & " is a prime number " & Wrap
End If
Next
currentPrime += 1
Loop
答案 0 :(得分:0)
试试这个......
Dim i, n, input As Integer
Dim Wrap As String = Chr(10) & Chr(13)
input = txtInput.Text
Dim found = 0
Dim output = "Prime Numbers are : " & Wrap
While found < input
i = i + 1
For j = 2 To Fix(i / 2) + 1
If i Mod j = 0 Then
n = 1
End If
Next
If n = 1 Then
n = 0
Else
output = output & Wrap & i & " is a prime number " & Wrap
found = found + 1
End If
End While
txtAns.Text = output