修复未来有问题的代码?
我是一个非常新的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_EQUALn" +
"This indicates that a kernel-mode driver attempted to access pageable memory atn" +
"a process IRQL that was too high.n";
else if (userInput == "0xa")
message = "IRQL_NOT_LESS_OR_EQUALn" +
"This indicates that Microsoft Windows or a kernel-mode driver accessedn" +
"paged memory at DISPATCH_LEVEL or above.n";
else if (userInput == "0x1e")
message = "KMODE_EXCEPTION_NOT_HANDLEDn" +
"This indicates that a kernel-mode program generated an exception which then" +
"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 accessedn" +
"paged memory at DISPATCH_LEVEL or above.n" },
{ "0xd1", "This indicates that a kernel-mode driver attempted to access pageable memory atn" +
"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;
}
}
}
所以我有点理解字典和类的基础知识,但是我仍然不知道如何在当前的“错误检查”程序中实现/更好的方法。
任何建议,提示? 我试图让我的帖子表明我尽可能地尝试自己弄明白,因为我知道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 accessedn" +
"paged memory at DISPATCH_LEVEL or above.n" },
{ "0xd1", "This indicates that a kernel-mode driver attempted to access pageable memory atn" +
"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>
是合理的 - 不需要使它更复杂。
你说“我不知道”,但你真的很接近。
链接地址: http://www.djcxy.com/p/27097.html