计算机vb试题及答案
- 格式:docx
- 大小:37.13 KB
- 文档页数:6
计算机vb试题及答案
1. 题目:使用编写一个程序,实现对一个整数数组的排序。
答案:
```
Module Module1
Sub Main()
Dim arr As New Integer() {5, 2, 9, 1, 5, 6}
Console.WriteLine("原始数组:")
PrintArray(arr)
Array.Sort(arr)
Console.WriteLine("排序后的数组:")
PrintArray(arr)
Console.ReadKey()
End Sub
Sub PrintArray(arr As Integer())
For Each i As Integer In arr
Console.Write("{0} ", i)
Next
Console.WriteLine()
End Sub
End Module
```
2. 题目:编写一个程序,计算并输出一个数的阶乘。
答案:
```
Module Module1
Sub Main()
Dim number As Integer = 5
Dim factorial As Long = Factorial(number)
Console.WriteLine("{0} 的阶乘是 {1}", number,
factorial)
Console.ReadKey()
End Sub
Function Factorial(n As Integer) As Long
If n = 0 Or n = 1 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
End Function
End Module
```
3. 题目:使用创建一个程序,该程序可以计算一个圆的面积。
答案:
```
Module Module1
Sub Main()
Dim radius As Double = 5.0
Dim area As Double = Math.PI * radius * radius
Console.WriteLine("半径为 {0} 的圆的面积是 {1}",
radius, area)
Console.ReadKey()
End Sub
End Module
```
4. 题目:编写一个程序,实现简单的字符串反转。
答案:
```
Module Module1
Sub Main()
Dim original As String = "Hello World"
Dim reversed As String = ReverseString(original)
Console.WriteLine("原始字符串: {0}", original)
Console.WriteLine("反转后的字符串: {0}", reversed)
Console.ReadKey()
End Sub
Function ReverseString(s As String) As String
Dim charArray() As Char = s.ToCharArray()
Array.Reverse(charArray)
Return New String(charArray)
End Function
End Module
```
5. 题目:使用编写一个程序,判断一个数是否为素数。
答案:
```
Module Module1
Sub Main()
Dim number As Integer = 29
If IsPrime(number) Then
Console.WriteLine("{0} 是素数", number)
Else
Console.WriteLine("{0} 不是素数", number)
End If
Console.ReadKey()
End Sub
Function IsPrime(n As Integer) As Boolean
If n <= 1 Then Return False
For i As Integer = 2 To Math.Sqrt(n)
If n Mod i = 0 Then Return False
Next
Return True
End Function
End Module
```
6. 题目:编写一个程序,实现斐波那契数列的前10个数。
答案:
```
Module Module1
Sub Main()
Dim fib As New List(Of Integer)
For i As Integer = 1 To 10
fib.Add(Fibonacci(i))
Console.Write(fib(i - 1) & " ")
Next
Console.WriteLine()
Console.ReadKey()
End Sub
Function Fibonacci(n As Integer) As Integer
If n <= 1 Then
Return n
Else
Return Fibonacci(n - 1) + Fibonacci(n - 2)
End If
End Function
End Module
```
7. 题目:使用创建一个程序,实现简单的文件复制。
答案:
``` Module Module1
Sub Main()
Dim sourcePath As String = "C:\source.txt"
Dim destinationPath As String =
"C:\destination.txt"
Try
File.Copy(sourcePath, destinationPath)
Console.WriteLine("文件复制成功")
Catch ex As Exception
Console.WriteLine("文件复制失败: " &
ex.Message)
End Try
Console.ReadKey()
End Sub
End Module
```
8. 题目:编写一个程序,实现一个简单的计算器。
答案:
```
Module Module1
Sub Main()
Dim num