簡體   English   中英

如果用戶在沒有輸入數字的情況下按下回車鍵,我該如何編寫錯誤消息。(例如輸入字母或什么都不輸入)

[英]How do I program an Error message if the user presses enter without putting a number in. (e.g. putting a letter or nothing at all)

 'the variables
        Dim watertemp As Decimal 'changed variable to decimal so that decimal numbers can be input.
        Dim fahren As Decimal
        Dim newtemp As String


        'this code asks the intial question (the temp of the water)
        Console.WriteLine("Enter the tempurature of the water, between -273.15 and 100 degrees celcius.")
        watertemp = Console.ReadLine





        'this code is to work out what the water should be called and the colour of the text it will be displayed in.
        If watertemp <= 0 Then
            Console.ForegroundColor = ConsoleColor.Blue
            Console.WriteLine("The water is freezing")
        End If
        If watertemp >= 1 And watertemp <= 25 Then
            Console.ForegroundColor = ConsoleColor.Cyan
            Console.WriteLine("The water is warm")
        End If
        If watertemp >= 26 And watertemp <= 60 Then
            Console.WriteLine("The water is hot")
        End If
        If watertemp >= 61 And watertemp <= 99 Then
            Console.ForegroundColor = ConsoleColor.Magenta
            Console.WriteLine("The water is very hot")
        End If
        If watertemp >= 100 Then
            Console.ForegroundColor = ConsoleColor.Red
            Console.WriteLine("The water is boiling")
        End If

        'this code converts the tempurature to fahrenheit
        fahren = (9 / 5 * watertemp) + 32
        Console.WriteLine("The water is " & (Format(fahren, "0.00")) & " °F")

        'this code changes the text back to white
        Console.ForegroundColor = ConsoleColor.White
        'this code asks the user if they would like to convert another input
        Console.WriteLine("Would you like to convert another tempurature?")
        newtemp = Console.ReadLine

        'this code determines whether to carry on with converting another input or end the program
        If newtemp = "no" Then
            Console.WriteLine("This is the end of the program")
        End If
        If newtemp = "yes" Then
            Console.WriteLine("What is the tempurature of the water?")
            watertemp = Console.ReadLine
        End If
        fahren = (9 / 5 * watertemp) + 32
        Console.WriteLine("The water is " & (Format(fahren, "0.00")) & " °F")

        Console.ReadLine()

如果用戶輸入了程序無法運行的內容,例如字母或什么都沒有,我需要有一條錯誤消息。

我嘗試了一些東西,但我不知道我是否做對了,因為我對編碼很陌生。

Dim watertemp As Decimal 'changed variable to decimal so that decimal numbers can be input.
Dim fahren As Decimal
Dim newtemp As String

reenter:
        'this code asks the intial question (the temp of the water)
        Console.WriteLine("Enter the tempurature of the water, between -273.15 and 100 degrees celcius.")
        watertemp = Console.ReadLine
        If String.IsNullOrEmpty(watertemp) OrElse Not IsNumeric(watertemp) Then
            MsgBox("Wrong input.")
            GoTo reenter
        End If 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM