用vba输出1到100间的质数
时间: 2023-02-27 12:07:22 浏览: 86
以下是一种使用 VBA 输出 1 到 100 之间质数的代码示例:
Sub PrintPrimes()
Dim i As Integer, j As Integer
For i = 2 To 100
For j = 2 To i - 1
If i Mod j = 0 Then
Exit For
End If
Next j
If i = j Then
Debug.Print i
End If
Next i
End Sub
该代码使用两重循环枚举数字,内部循环用于检查外部循环的数字是否为质数,如果是,则使用 Debug.Print 输出该数字。
相关问题
用 VBA代码写1加到100
以下是用 VBA 代码实现将1加到100的程序:
```
Sub sum()
Dim i As Integer
Dim sum As Integer
sum = 0
For i = 1 To 100
sum = sum + i
Next i
MsgBox "1+2+...+100=" & sum
End Sub
```
运行此代码后,会弹出一个消息框,显示结果为5050。
写个1到100的排序的VBA程序
Sub sort()
Dim arr(1 To 100) As Integer
Dim i As Integer, j As Integer, temp As Integer
'生成1到100的随机数
For i = 1 To 100
arr(i) = Int(Rnd() * 100) + 1
Next i
'冒泡排序
For i = 1 To 99
For j = i + 1 To 100
If arr(i) > arr(j) Then
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next j
Next i
'输出排序结果
For i = 1 To 100
Debug.Print arr(i)
Next i
End Sub
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)