VB.Net Tutorial/Language Basics/Console Read

Материал из VB Эксперт
Версия от 12:54, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Convert what you type to value type

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
Enter a temperature...
12
Too cold!

Match a pattern from user input

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
Please Enter A Pattern:*
Please Enter A String To Compare Against:123
123 Matched with *

Read a complete line of text

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
Enter a line of text: a line
You just entered: a line

Read a single character

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
Enter a character: a
You just entered a.

Read a string from console

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
HELLO

Read keyboard input

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
Enter a source temperature.
> 1
1

Use Do Loop to read user input

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
Please enter "q" to quit...
q
You typed q
Quitting now.

Use While to read user input

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
Please enter "q" to quit...
q
Quitting now.