首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修复未来的有问题的代码?

修复未来的有问题的代码?
EN

Stack Overflow用户
提问于 2015-06-22 02:11:15
回答 1查看 129关注 0票数 7

我是一个非常新的C#程序员,不到24小时的原始输入/编程。我会说一个星期左右,然而,阅读,观看,并试图理解的东西。

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

代码语言:javascript
复制
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);
        }
    }
}
}

这个程序完全符合我的要求。用户键入他们正在获得的bug检查代码,它将返回非常基本的MSDN-MSFT定义,并要求您输入另一个bug检查代码,或者退出退出程序。

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

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

代码语言:javascript
复制
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();
    }
}
}

现在我一直在问自己的问题是“我是怎么把这个应用到我的当前代码中的?”,我仍然不知道该说什么让我感到羞愧。我尝试过查找和研究创建/管理新类,虽然这有点令人困惑,但我了解了它的基本概念,并编写了一个快速程序,它不要求用户输入,而是打印到控制台上:

代码语言:javascript
复制
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;
    }
}

}

因此,我有点了解字典和类的基本知识,但我仍然不知道如何在我当前的"bug检查“程序中实现/更好的方法。

有什么建议吗小费?我试着写我的帖子,以表明我正在尽我自己最大的努力去解决这个问题,因为我知道StackOverflow皱眉反对那些只想让别人为他们编码的人,我只是不知道还有什么地方可以看/读来找出答案,或者如何让它至少朝着正确的方向前进。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-22 02:24:54

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

代码语言:javascript
复制
    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]来获取值。如果不显示错误,则显示错误。

您根本不需要自定义类来实现您自己的字典。字典只是KeyValuePairs的一个泛型集合。在这里使用Dictionary<string, string>是有意义的--不需要让它变得更加复杂。

你说“我不知道”,但你真的很亲密。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30971265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档