修复未来有问题的代码?

时间:2015-06-22 02:11:15

标签: c#

我是一名非常新的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定义,并要求您键入另一个错误检查代码,或退出以退出程序。

然而,根据视频/阅读材料,要学习的重要一点是如何发现将来会成为问题的代码。好吧,至少从我的角度来看,我用来检查用户关于错误检查的输入值的方法可能不太好,因为它最终会方式太大而且杂乱,因为有错误检查的数量。

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

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();
    }
}
}

现在对我自己的问题一直是&#34;我如何将其实现到我当前的代码中?&#34;,我仍然不知道哪些让我感到羞耻。我已经尝试过查找并研究创建/管理新类,虽然这有点令人困惑,但我得到了它的基本概念并编写了一个不会要求用户输入的快速程序,而只是打印到控制台:

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;
    }
}

}

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

任何建议,提示?我试图让我的帖子显示我尽可能多地尝试自己解决这个问题,因为我知道StackOverflow对人们只是试图让别人为他们编码感到皱眉,我只是不确定在哪里看/读出来,或者至少如何朝着正确的方向前进。

1 个答案:

答案 0 :(得分:3)

这是一个非常简单的词典实现:

    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>是有道理的 - 不需要让它更复杂。

你说“我不知道”,但你真的非常接近。