我正在尝试将此代码转换为VB.NET,但我不知道如何。
Pyhton代码:
import math
skippie=raw_input("If you know how to do this, enter skip ")
if skippie=="skip":
found = False
varmin=1
varmax=100
while not found:
guess = (varmax+varmin)/2
ans=raw_input("Is your input larger (l), smaller(s), or equal to (e) than " + str(guess) + " ")
if ans == "L" or ans == "l":
varmin = guess
elif ans == "S" or ans == "s":
varmax = guess
else:
print "Yay! I got it!"
found = True
Pastebin代码:pastebin link
答案 0 :(得分:1)
我认为这段代码可以改进,但我认为保持它与原始代码非常接近会更有帮助:
Console.WriteLine("If you know how to do this, enter skip")
Dim skippie As String = Console.ReadLine()
If skippie = "skip" Then
Dim found As Boolean = False
Dim varmin As Integer = 1
Dim varmax As Integer = 100
While Not Found
Dim guess As Integer = (varmax + varmin) / 2
Console.WriteLine("Is your input larger than (l), smaller than (s), or equal to (e) {0}?", guess.ToString)
Dim ans As String = Console.ReadLine()
If ans = "L" Or ans = "l" Then
varmin = guess
ElseIf ans = "S" Or ans = "s" Then
varmax = guess
Else
Console.WriteLine("Yay! I got it!")
found = True
End If
End While
End If
下次,请尝试自行翻译代码,然后发布并发布您的尝试结果。
编辑: Windows表单版本
If InputBox("If you know how to do this, enter skip") = "skip" Then
Dim varmin As Integer = 1
Dim varmax As Integer = 100
Do
Dim guess As Integer = (varmax + varmin) / 2
Select Case UCase(InputBox("Is your input larger than (l), smaller than (s), or equal to (e) " & guess.ToString & "?"))
Case "L"
varmin = guess
Case "S"
varmax = guess
Case "E"
MsgBox("Yay! I got it!")
Exit Do
End Select
Loop
End If
我也对这个版本做了一些风格上的修改。 (大多数情况下我删除了不必要的变量。)行为是一样的,但在我看来,第二个版本更多vb.net,而第一个版本更多python。