簡體   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