2017-10-15 112 views
0

我对编程非常陌生,目前只是搞乱了控制台应用程序。我创造了一些东西,例如登录屏幕和货币转换器,但是这是在老师的帮助下完成的。从来没有做过任何事情。有没有更短的写这段代码的方法?

我想知道是否有更好/更短的写作方式? 模块模块1

Sub Main() 
    Dim x As String 
    Dim Y As String 
    Dim yes As String 
    Dim no As String 
    x = "Please enter your name:" 
    Y = "Please enter 'Y' or 'N'" 
    yes = "Y" 
    no = "N" 
    Console.WriteLine(x) 
    Console.ReadLine() 
    Console.WriteLine("Do you wish to continue?") 
    yes = Console.ReadLine() 
    Console.WriteLine(Y) 
    If yes = "Y" Then 
     Console.WriteLine("You selected to continue") 
    Else 
     If no = "N" Then 
      Console.WriteLine("You selected to exit") 
      Environment.Exit(0) 
     End If 
    End If 
    Console.WriteLine("TEXT HERE") 'Text here as I don't know what to put next yet 
    Console.ReadLine() 
    Console.ReadLine() 'Just put this here so it doesn't exit straight away 
End Sub 

我已经宣布了一些变量只是尝试一下,而不是仅仅有Console.WriteLine(“文本”)不断。我只是想找到办法。 我刚刚再次运行了代码,发现它对用户输入区分大小写,我该如何处理它是Y还是Y和N或n?

+0

我投票结束这个问题作为题外话,因为它要求审查。请参阅https://www.codereview.stackexchange.com – Codexer

回答

0

您可以使用下面的代码:

Sub Main() 
    Console.WriteLine("Please enter your name:") 
    Console.ReadLine() 
    Console.WriteLine("Do you wish to continue?") 

    Do 
     Dim selectYN As String = Console.ReadLine() 

     If selectYN.ToUpper = "Y" Then 
      Console.WriteLine("You selected to continue") 
      Exit Do 
     ElseIf selectYN.ToUpper = "N" Then 
      Console.WriteLine("You selected to exit") 
      Environment.Exit(0) 
      Exit Do 
     Else 
      Console.WriteLine("Please enter 'Y' or 'N'") 
     End If 
    Loop 

    Console.WriteLine("TEXT HERE") 'Text here as I don't know what to put next yet 
    Console.ReadLine() 
    Console.ReadLine() 'Just put this here so it doesn't exit straight away 
End Sub 

比你的代码的代码更精缩。我还添加了一个循环,直到用户为是/否问题添加了有效答案。用户必须输入以下值之一来打破循环:n, N, y, Y。如果该值无效,问题再次出现给他一次再次输入新值的机会。

我该怎么做,要么是Y或Y,N或n?

在这种情况下,你必须转换信toLowertoUpper的可能性。在上面的示例中,toUpper用于检查NY

相关问题