繁体   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