简体   繁体   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()

I need to have an error message if the user inputs something that the program cannot run, such as letters or nothing at all.如果用户输入了程序无法运行的内容,例如字母或什么都没有,我需要有一条错误消息。

I tried a few things but I dont know if I did any of them right as I am very new to coding.我尝试了一些东西,但我不知道我是否做对了,因为我对编码很陌生。

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.

相关问题 如果用户未输入整数,如何将错误消息添加到程序中? - How can I add an error message to my program if the user doesn't enter an integer? 如何进行格式检查? 所以输入的格式正确。 例如日/月/年。 (Visual Basic) - How do i do a format check? so the input is in the correct format. E.g. dd/mm/yy. (Visual Basic) 如何在 For 循环中增加变量的名称,例如 bar1、bar2、bar3、VBNET? - How do I increment the name of a variable e.g. bar1, bar2, bar3, VBNET in a For loop? 创建一个可以从给定图像中选择一个图像的程序是否可行? 例如,螺旋星系与椭圆星系 - Is it feasable to create a program that can select an image out of a number of images given. e.g. a spiral galaxy versus an ellpictcal one 大括号中的数字例如是什么? “{0}”是什么意思? - What do numbers in braces e.g. “{0}” mean? 如何在VB中制作自动打字机,而不是仅放一段文本,而是如何逐个字母地键入? - How to make an Auto Typer in VB, but instead of just putting a block of text, how to type it letter by letter? 如果程序未运行,如何显示错误消息? - How do I show an error message if a program is not running? 如果用户未在多行文本框的第二行输入文本,如何添加错误消息? - How can I add an error message when user does not enter text into second line of multiple line textbox? 我需要帮助以正确的顺序排列所有逻辑语句 - I need help putting all the logic statements in the correct order 如果用户在文本框中按下Enter键,则捕获 - Catch if user presses enter key in textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM