修复未来有问题的代码

本文关键字:代码 有问题 未来 | 更新日期: 2023-09-27 17:57:31

我是一个非常新的C#程序员,实际打字/编程时间不到24小时。然而,我想说大约一周的阅读、观看和尝试理解事物。

这是我的第一个程序的源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bugcheck
{
class Program
{
    static void Main(string[] args)
    {
        // Initializing variables
        string bugcheckExit = "exit";
        var message = "";
        var afterMessage = "Type another bug check, or '"exit'" to quit:'n";
        Console.BackgroundColor = ConsoleColor.Blue;
        Console.Clear();
        // Write welcome message to screen
        Console.WriteLine("Type a bug check code (for example - 0xD1), or '"exit'" to quit:'n");
        while (true)
        {               
            // Take in user input
            string userInput = Console.ReadLine().ToLower();
            // Check user input
            if (userInput == bugcheckExit)
                System.Environment.Exit(1);
            // Checking value of bug check
            if (userInput == "0xd1")
                message = "DRIVER_IRQL_NOT_LESS_OR_EQUAL'n" +
                    "This indicates that a kernel-mode driver attempted to access pageable memory at'n" + 
                    "a process IRQL that was too high.'n";
            else if (userInput == "0xa")
                message = "IRQL_NOT_LESS_OR_EQUAL'n" +
                    "This indicates that Microsoft Windows or a kernel-mode driver accessed'n" +
                    "paged memory at DISPATCH_LEVEL or above.'n";
            else if (userInput == "0x1e")
                message = "KMODE_EXCEPTION_NOT_HANDLED'n" +
                    "This indicates that a kernel-mode program generated an exception which the'n" +
                    "error handler did not catch.'n";
            else
                message = "Not a valid bug check, please try again.'n";
            Console.WriteLine(message);
            Console.WriteLine(afterMessage);
        }
    }
}
}

该程序完全按照我的意愿执行。用户键入他们得到的错误检查代码,它会返回非常基本的MSDN-MSFT定义,并要求您键入另一个错误检查代码或退出以退出该程序。

然而,根据视频/阅读材料,需要学习的一件重要事情是如何发现未来将成为问题的代码。好吧,至少从我的角度来看,我用来检查用户输入的关于bug检查的值的方法可能不好,因为考虑到bug检查的数量,它最终会变得太大和混乱。

所以我做了一些研究,发现了一些字典,很整洁。我想出了这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dictionary
{
class Program
{
    static void Main(string[] args)
    {
        string value = "";
        Dictionary<string, string> openWith = new Dictionary<string, string>
{
    { "0xa", "This indicates that Microsoft Windows or a kernel-mode driver accessed'n" + 
            "paged memory at DISPATCH_LEVEL or above.'n" },
    { "0xd1", "This indicates that a kernel-mode driver attempted to access pageable memory at'n" + 
            "a process IRQL that was too high.'n" }
};
            if (openWith.TryGetValue("0xd1", out value))
            {
                Console.WriteLine(value);
            }
            else
            {
                Console.WriteLine(" is not found");
            }
            Console.ReadLine();
    }
}
}

现在,我一直在思考的问题是"我到底该如何将其实现到我当前的代码中?",我仍然不知道该说什么让我感到羞愧。我试着查找和研究创建/管理新类,尽管这有点令人困惑,但我已经了解了它的基本概念,并编写了一个不需要用户输入,而是只打印到控制台的快速程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GPU
{
class Program
{
    static void Main(string[] args)
    {
        GPU myNewGPU = new GPU();
        myNewGPU.Make = "nVidia";
        myNewGPU.Model = "GTX 970";
        myNewGPU.Year = 2014;
        myNewGPU.Color = "Black";
        Console.WriteLine("{0} - {1} - {2}",
            myNewGPU.Make,
            myNewGPU.Model,
            myNewGPU.Color);
        Console.WriteLine("GPU's value: {0:G}",  myNewGPU.DetermineMarketValue());
        Console.ReadLine();
    }
}
class GPU
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public string Color { get; set; }
    public double DetermineMarketValue()
    {
        double GPUValue = 100.0;
        if (this.Year > 2010)
            GPUValue = 400.0;
        else
            GPUValue = 100.0;
        return GPUValue;
    }
}
}

所以我有点理解字典和类的基本知识,但我仍然不知道如何在我当前的"错误检查"程序中实现/a更好的方法。

有什么建议吗?我试图在我的帖子中表明,我正在尽可能地自己解决这个问题,因为我知道StackOverflow对那些试图让其他人为他们编码的人很反感,我只是不确定还有什么地方可以看/读,或者如何至少朝着正确的方向前进。

修复未来有问题的代码

以下是字典的一个非常简单的实现:

    static void Main(string[] args)
    {
        Dictionary<string, string> openWith = new Dictionary<string, string>
        {
            { "0xa", "This indicates that Microsoft Windows or a kernel-mode driver accessed'n" + 
                    "paged memory at DISPATCH_LEVEL or above.'n" },
            { "0xd1", "This indicates that a kernel-mode driver attempted to access pageable memory at'n" + 
                    "a process IRQL that was too high.'n" }
        };
        string userInput = "";
        while ((userInput = Console.ReadLine().ToLower()) != "exit")
        {                
            if (openWith.ContainsKey(userInput))
                Console.WriteLine(openWith[userInput]);
            else
                Console.WriteLine("Doesn't exist");
            Console.WriteLine("Type another bug check, or '"exit'" to quit:'n");
        }
    }

使用ContainsKey检查用户是否输入了字典中的值。如果是,请使用索引openWith[userInput]来获取该值。如果未显示错误。

决不需要自定义类来实现自己的字典。Dictionary只是KeyValuePairs的通用集合。在这里使用Dictionary<string, string>是有意义的,无需使其更加复杂。

你说"我不知道",但你真的很接近。