gpt4 book ai didi

c# - 修复 future 有问题的代码?

转载 作者:太空狗 更新时间:2023-10-29 20:33:33 26 4
gpt4 key购买 nike

<分区>

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

但是,根据视频/阅读 Material ,要学习的重要一件事是如何发现将来会出现问题的代码。好吧,至少从我的角度来看,我用来检查用户关于错误检查的输入值的方法可能并不好,因为它最终会方式太大并且考虑到错误检查的数量,这很困惑。

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

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

}

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

有什么建议、技巧吗?我试图让我的帖子表明我正在尽可能多地尝试自己解决这个问题,因为我知道 StackOverflow 不喜欢那些试图让其他人为他们编写代码的人,我只是不确定还有什么地方可以看/阅读以弄清楚,或者至少如何朝着正确的方向前进。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com