你能给我一些vb.net比较好玩的代码吗
时间: 2024-02-01 11:45:38 浏览: 153
VB.NET 窗体圆角代码 亲测能用
当然可以!以下是一些VB.NET比较有趣的代码示例:
1. 计算阶乘
```
Private Function Factorial(ByVal num As Integer) As Integer
If num <= 1 Then
Return 1
Else
Return num * Factorial(num - 1)
End If
End Function
```
2. 猜数字游戏
```
Dim randomNum As New Random()
Dim secretNumber As Integer = randomNum.Next(1, 101)
Dim guess As Integer = 0
Dim numGuesses As Integer = 0
Do While guess <> secretNumber
guess = InputBox("Guess a number between 1 and 100.")
numGuesses += 1
If guess < secretNumber Then
MessageBox.Show("Too low! Guess again.")
ElseIf guess > secretNumber Then
MessageBox.Show("Too high! Guess again.")
End If
Loop
MessageBox.Show("Congratulations! You guessed the secret number in " & numGuesses & " guesses.")
```
3. 滚动文字
```
Private Sub scrollText()
Dim text As String = "Hello, world!"
Dim speed As Integer = 100
Dim i As Integer = 0
Do While i < text.Length
lblOutput.Text = text.Substring(i) + text.Substring(0, i)
i += 1
Thread.Sleep(speed)
Loop
End Sub
```
4. 简单的计算器
```
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim num1 As Integer = CInt(txtNum1.Text)
Dim num2 As Integer = CInt(txtNum2.Text)
Dim result As Integer
Select Case cboOperator.Text
Case "+"
result = num1 + num2
Case "-"
result = num1 - num2
Case "*"
result = num1 * num2
Case "/"
result = num1 / num2
End Select
lblResult.Text = result.ToString()
End Sub
```
这些示例只是VB.NET的冰山一角,还有很多其他有趣和有用的代码示例可以探索!
阅读全文