将Python代码反转为VB.NET版

2024-09-30 20:32:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把这段代码翻译成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 link


Tags: orto代码inputrawnetifvb
1条回答
网友
1楼 · 发布于 2024-09-30 20:32:31

我认为这段代码可以改进,但我认为将其与原始代码保持非常接近会更有帮助:

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

我也对这个版本做了一些风格上的修改。(我基本上消除了不必要的变量)行为是相同的,但在我看来,第二个版本更,而第一个版本更。你知道吗

相关问题 更多 >