2015-06-22 50 views
7

我是一個非常新的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(); 
    } 
} 
} 

現在的問題對自己的整段時間一直以「如何赫克我實現這個成爲我當前的代碼?」,我還是真的不知道這使我感到慚愧,以說。我嘗試過查找和研究創建/管理新類,雖然它有點令人困惑,但我已經瞭解了它的基本想法,並編寫了一個快速程序,它不要求用戶輸入,而只是打印到控制檯:

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只是讓人們試圖讓別人爲他們編碼,我只是不確定在哪裏看/閱讀弄清楚,或者至少如何將其推向正確的方向。

+0

這是那種地方。不知道最後三分之一是關於自定義類的。你是否想要將你的字典實現到你已有的代碼中?你有什麼問題? – Jonesopolis

+1

雖然我(也可能是我們其他人)真的很感激這麼做了多少努力,但我們需要一個具體的問題。它是如何使用字典?如何重構現有的代碼來利用字典?上帝保佑,哪裏可以找到教程? –

+0

對不起,第三個(自定義類)在那裏,因爲我認爲我需要理解類來實現字典到我已經工作的代碼中。我遇到的主要問題是(在這裏重複自己)我最終不知道如何將執行字典實現到我已有的代碼中。 – ajdbnabad13

回答

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]來獲取該值。如果不顯示錯誤。

絕對不需要自定義類來實現自己的字典。字典只是KeyValuePairs的通用集合。在這裏使用Dictionary<string, string>是有道理的 - 無需使它更復雜。

你說「我不知道」,但你真的很接近。