繁体   English   中英

在转换程序C#Visual Studio中进行编程时的错误消息

[英]Error messages in programming in a conversion program c# Visual studio

我是编程的新手,我只进行了大约几周的编程,所以我不确定如何为程序编写错误消息。 谁能帮助我为该转换程序提供一些错误消息?

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Converting_Temperature
{
class Program
{
    static double ConvertIToCentimeters()
    {
        Console.Write("Please input the Inches you would like to convert to     Centimeters: ");
        double Centimeters = Convert.ToDouble(Console.ReadLine());
        double Inches = (double)Centimeters * 2.54;
        return Inches;

    }

    static double ConvertIToMillimeters()
    {
        Console.Write("Please input the Inches you would like to convert to Millimeters: ");
        double Millimeters = Convert.ToDouble(Console.ReadLine());
        double Inches = (double)Millimeters * 25.4; 
        return Inches; 
    }

    static double ConvertIToMeters()
    {
        Console.Write("Please input the Inches you would like to convert to Meters: ");
        double Meters = Convert.ToDouble(Console.ReadLine());
        double Inches = (double)Meters * 0.02540000; 
        return Inches; 
    }

    static double ConvertIToFoot()
    {
        Console.Write("Please input the Inches you would like to convert to Foot: ");
        double Foot = Convert.ToDouble(Console.ReadLine());
        double Inches = (double)Foot * 0.0833333; 
        return Inches;  
    }

    static double ConvertIToHelp()
    {
        Console.Write("\n Help!\n 1.To convert from Inches to Centimetres you must select option 1 from the menu. The program will ask you to input the amount of inches you would like to convert. Once you enter the amount the program will convert it into Centimetres.\n 2.To convert from Inches to Millimetres you must select option 1 from the menu. The program will ask you to input the amount of inches you would like to convert. Once you enter the amount the program will convert it into Millimetres.\n 3.To convert from Inches to Feet you must select option 1 from the menu. The program will ask you to input the amount of inches you would like to convert. Once you enter the amount the program will convert it into Feet.\n 4.To convert from Inches to Metres you must select option 1 from the menu. The program will ask you to input the amount of inches you would like to convert. Once you enter the amount the program will convert it into Metres.\n 5.For any inquires or problems you may have about the program please contact me on my email at victor.pietrasiak@gmail.com or call this number 0123 323 324."); //tells the user about how to use the program
        double Help = Convert.ToDouble(Console.ReadLine());
        return Help; 
    }

    static int Menu()
    {
        Console.WriteLine("Please choose one of these options");
        Console.WriteLine("1.Inches to Centimeters");
        Console.WriteLine("2.Inches to Millimeters");
        Console.WriteLine("3.Inches to Meters");
        Console.WriteLine("4.Inches to Feet");
        Console.WriteLine("5.Help");
        Console.WriteLine("6.Quit");
        int choice = Convert.ToInt32(Console.ReadLine());
        return choice; 
    }






    static void Main(string[] args)
    {
        int menuChoice = Menu();
        if (menuChoice == 1)
        {
            double result = ConvertIToCentimeters();
            Console.WriteLine("The result in Centimeters is {0}", result);
        }

        if (menuChoice == 2)
        {
            double result = ConvertIToMillimeters();
            Console.WriteLine("The result in Millimeters is {0}", result);

        }

        if (menuChoice == 3)
        {
            double result = ConvertIToMeters();
            Console.WriteLine("The result in Meters is {0}", result);

        }

        if (menuChoice == 4)
        {
            double result = ConvertIToFoot();
            Console.WriteLine("The result in Foot is {0}", result);

        }

        if (menuChoice == 5)
        {
            double result = ConvertIToHelp();
            Console.WriteLine("Help {0}", result);
        }

        if (menuChoice < 1) 
        {
            throw new ApplicationException("You have entered a number less than 1"); //this was my attempt on making an error message
        }


    }

}

}

由于要创建控制台应用程序,因此可以像对待普通消息一样直接将错误写入控制台。

因此,而不是throw new ApplicationException("You have entered a number less than 1");

您可以使用:

Console.WriteLine("Error: You have entered a number less than 1");

您可以输出如下错误:

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You have entered a number less than 1");
Console.ResetColor();

那么您的错误将以红线打印出来,之后您可以关闭应用程序或重新启动它。

引发异常主要是关于处理通常无法预料的异常情况。 在您的情况下,您预计用户的输入可能小于1,在这种情况下,您应该仅告知该数字在某种程度上不正确。 为此使用Console.WriteLine或类似的方法。

异常更多是关于使使用您的代码的开发人员能够处理方法内部发生的不可预测的行为(例如,他们可能想捕获您抛出的异常并清理资源),或者,如果情况非常严重,则可以立即使用关闭应用程序以防止状态损坏/安全漏洞。 不要过度使用它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM