VB.Net Tutorial/Language Basics/Console Read — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:54, 26 мая 2010
Содержание
Convert what you type to value type
<source lang="vbnet">Module Module1
Sub Main() Dim intInput As Integer Console.WriteLine("Enter a temperature...") intInput = Val(Console.ReadLine()) If intInput > 75 Then Console.WriteLine("Too hot!") ElseIf intInput < 55 Then Console.WriteLine("Too cold!") Else Console.WriteLine("Just right!") End If End Sub
End Module</source>
Enter a temperature... 12 Too cold!
Match a pattern from user input
<source lang="vbnet">Public Class PatternMatcher
Shared Sub Main() Dim sInput As String Dim sPattern As String Dim sMatch As String System.Console.Write("Please Enter A Pattern:") sInput = System.Console.ReadLine() sPattern = sInput System.Console.Write("Please Enter A String To Compare Against:") sInput = System.Console.ReadLine() sMatch = sInput If sMatch Like sPattern Then System.Console.WriteLine(sMatch & " Matched with " & sPattern) Else System.Console.WriteLine(sMatch & " did not Match with " & sPattern) End If End Sub
End Class</source>
Please Enter A Pattern:* Please Enter A String To Compare Against:123 123 Matched with *
Read a complete line of text
<source lang="vbnet">Module Module1
Sub Main() Dim strLine As String Console.Write("Enter a line of text: ") strLine = Console.ReadLine Console.WriteLine() Console.WriteLine("You just entered: {0}", strLine) End Sub
End Module</source>
Enter a line of text: a line You just entered: a line
Read a single character
<source lang="vbnet">Module Module1
Sub Main() Dim strChar As String Console.Write("Enter a character: ") strChar = Console.ReadKey.KeyChar "strChar = Console.ReadKey(True).KeyChar Console.WriteLine() Console.WriteLine("You just entered {0}.", strChar)
End Sub
End Module</source>
Enter a character: a You just entered a.
Read a string from console
<source lang="vbnet">Module Module1
Sub Main() Dim strMessage As String Try strMessage = Console.ReadLine() Console.WriteLine("HELLO " + strMessage) Catch ex As Exception End Try End Sub
End Module</source>
HELLO
Read keyboard input
<source lang="vbnet">Class Tester
Shared Sub Main() Dim userInput As String Console.WriteLine("Enter a source temperature.") userInput = Console.ReadLine() Console.WriteLine(userInput) End Sub
End Class</source>
Enter a source temperature. > 1 1
Use Do Loop to read user input
<source lang="vbnet">Module Module1
Sub Main() Dim strInput As String Do Console.WriteLine("Please enter "q" to quit...") strInput = Console.ReadLine() Console.WriteLine("You typed " & strInput) Loop While (strInput <> "q") Console.WriteLine("Quitting now.") End Sub
End Module</source>
Please enter "q" to quit... q You typed q Quitting now.
Use While to read user input
<source lang="vbnet">Module Module1
Sub Main() Console.WriteLine("Please enter "q" to quit...") Dim strInput As String = Console.ReadLine() While (strInput <> "q") Console.WriteLine("You typed " & strInput) Console.WriteLine("Please enter "q" to quit...") strInput = Console.ReadLine() End While Console.WriteLine("Quitting now.") End Sub
End Module</source>
Please enter "q" to quit... q Quitting now.