2014-10-17 130 views
0

好了,所以我想提出一個程序來驗證一個4位數字代碼。C#代碼驗證程序

計算機在一4位代碼生成一個4位代碼的 用戶類型。他們的猜測。 電腦告訴他們有多少數字是 在正確的位置和多少位已被 猜中,但在錯誤的地方猜中。 用戶獲得12個猜測來贏得比賽 - 猜猜正確的代碼。或者 失敗 - 無法猜測。

所以基本上,我的計劃似乎並沒有真正驗證碼是否正確,但我不能明白爲什麼不能因爲我有,如果和循環驗證,請看一看。

class Program 
{ 
    public static Random random = new Random(); 
    static void Main(string[] args) 
    { 
     int DigitOne = random.Next(0, 10); 
     int DigitTwo = random.Next(0, 10); 
     int DigitThree = random.Next(0, 10); 
     int DigitFour = random.Next(0, 10); 

     byte[] code = new byte[4]; 
     code[0] = Convert.ToByte(DigitOne); 
     code[1] = Convert.ToByte(DigitTwo); 
     code[2] = Convert.ToByte(DigitThree); 
     code[3] = Convert.ToByte(DigitFour); 
     bool CodeCorrect = false; 
     Console.WriteLine(code[0] +""+ code[1] +""+ code[2]+""+code [3]); 

     Console.WriteLine("You have 12 guesses before you will be permenantly locked out.\n"); 
     int AmountOfGuesses = 0; 
     while (AmountOfGuesses < 12 && !CodeCorrect) 
     { 
      Console.WriteLine("Enter 4 digit code to unlock the safe: "); 
      int[] UserCode = new int[4]; 

      for (int i = 0; i < 4; i++) 
      { 
       UserCode[i] = Convert.ToInt32(Console.Read()) - 48; 
      } 
      if (UserCode.Length != 4) 
      { 
       Console.WriteLine("Error. Try Again.\n"); 
      } 
      else 
      { 
       int UserDigitOne = UserCode[0]; 
       int UserDigitTwo = UserCode[1]; 
       int UserDigitThree = UserCode[2]; 
       int UserDigitFour = UserCode[3]; 
       for (int i = 0; i < 4; i++) 
       { 
        if (UserCode[i] == code[i]) 
        { 
         Console.WriteLine("The digit at position " + (i + 1) + " is correct."); 
        } 
       } 


       if (UserCode[0] == code[0] && UserCode[1] == code[1] && UserCode[2] == code[2] && UserCode[3] == code[3]) 
       { 
        CodeCorrect = true; 
        Console.WriteLine("Code Correct. Safe unlocked."); 

       } 
      } 



      AmountOfGuesses++; 
     } 

     if (AmountOfGuesses > 12) 
     { 
      Console.WriteLine("Code Incorrect. Safe Locked permenantly."); 
     } 

     Console.ReadLine(); 

    } 
+0

你有沒有通過代碼加強,看看發生了什麼? – 2014-10-17 18:01:17

+0

是的,我有... – user3063533 2014-10-17 18:01:48

+0

不知道爲什麼你需要使用'byte' ... – crashmstr 2014-10-17 18:04:35

回答

1

如果通過代碼產生它的數目1246,然後輸入命令行相同數目後,將其轉換爲字符數組,則每個字符轉換爲字節,就會得到以下四種字節:

這些對應於每個字符的ASCII表示,而不是實際的數字。

嘗試這樣:

int[] input = new int[4]; 

for(int i = 0; i < 4; i++) 
{ 
    input[i] = Convert.ToInt32(Console.Read()) - 48; 
} 

-48應該把你的ASCII碼爲系統提供的實際數字表示。 Console.Read()讀取單個字符而不是整行。

而且,你不必說了:

CodeCorrect == false 

這是更簡單地表示爲:

!CodeCorrect 

同樣,如果它被設置爲true,這純粹是:

CodeCorrect 

我也建議使用for循環來設置數組中的多個元素,而不是手動寫出每行的c頌。對於小型陣列來說這不是什麼大問題,但這是很好的做法。

更新:下面是完整的程序的修訂版:

class Program 
{ 
    public static Random random = new Random(); 
    static void Main(string[] args) 
    { 
     int[] randCombination = new int[4]; 
     for (int i = 0; i < 4; i++) 
     { 
      randCombination[i] = random.Next(0, 10); 
      Console.Write(randCombination[i].ToString()); 
     } 
     bool CodeCorrect = false; 

     Console.WriteLine("\nYou have 12 guesses before you will be permenantly locked out.\n"); 

     int AmountOfGuesses = 0; 
     while(AmountOfGuesses < 12 && !CodeCorrect) 
     { 
      Console.WriteLine("Enter 4 digit code to unlock the safe: "); 

      int[] UserCode = new int[4]; 
      string input = Console.ReadLine(); 

      int n; 
      bool isNumeric = int.TryParse(input, out n); 

      int correctCount = 0; 

      if(input.Length != 4 || !isNumeric) 
      { 
       Console.WriteLine("Error. Input code was not a 4 digit number.\n"); 
      } 
      else 
      { 
       for(int i = 0; i < 4; i++) 
       { 
        UserCode[i] = Convert.ToInt32(input[i]) - 48; 

        if(UserCode[i] == randCombination[i]) 
        { 
         Console.WriteLine("The digit at position " + (i + 1) + " is correct."); 
         correctCount++; 
        } 
       } 

       if(correctCount == 4) 
       { 
        CodeCorrect = true; 
        Console.WriteLine("Code Correct. Safe unlocked."); 
       } 
      } 
      AmountOfGuesses++; 
     } 

     if(AmountOfGuesses >= 12) 
     { 
      Console.WriteLine("Code Incorrect. Safe Locked permenantly."); 
     } 

     Console.ReadLine(); 
    } 
} 

幾件事情發生了變化:

  • 增加了在產生隨機數的上面的循環,進入它轉換爲整數數組,然後將其打印到標準輸出。
  • 我改變了用戶輸入讀回Console.ReadLine()的方式。其原因是要檢查用戶是否輸入了四位整數。 int.TryParse語句確保輸入是一個int,並且Length屬性檢查長度。
  • 我還使用了一個計數器來計算每個正確的猜測。如果進行了4次正確的數字猜測,保險櫃將被解鎖。
  • 您的最終if語句絕不會進行評估,因爲猜測量等於12,不會大於它。將其更改爲> =來自>。總是在尋找這樣的小事情。

編輯#2:有關int.TryParse的更多信息,請參閱以下內容:

+0

我已經實現了您所說的更改,並且在某種程度上它可以工作,但它仍然無法正確識別每個值,我編輯了代碼中的問題,請你再看一遍,並試着解釋爲什麼,抱歉只是因爲我正在學習c#並想了解我的程序中出錯的位置我可以在將來實施行動。 – user3063533 2014-10-17 19:32:40

+0

你可以添加你的修改後的代碼到你的文章中,以便我們看看它嗎? – CynicalProgrammer 2014-10-17 19:35:20

+0

我已經做了,我現在把代碼放在 – user3063533 2014-10-17 19:35:57

1

您正在比較數字與數字的字符表示形式。 code[]的每個值代表一個實際的數字。然後,將這些值與UserCode中的值進行比較,這是一個字符串,這意味着每個索引處都有一個字符。 ((byte)'4') == ((byte)4)(使用4作爲例子,但適用於任何數字數字)絕不是這種情況。解決此

的一種方式是對每個用戶輸入的字符解析成使用byte.Parse方法的一個字節。


爲了好玩,學習的目的看,從下面的代碼輸出:

for (char i = '0'; i <= '9'; i++) 
{ 
    Console.WriteLine("char: " + i + "; value: " + ((byte)i)); 
} 

輸出居然是:

char: 0; value: 48 
char: 1; value: 49 
char: 2; value: 50 
char: 3; value: 51 
char: 4; value: 52 
char: 5; value: 53 
char: 6; value: 54 
char: 7; value: 55 
char: 8; value: 56 
char: 9; value: 57 

這是由於編碼字符串。

我還建議您讓自己的代碼正常工作,將其提交給Code Review網站上的優秀人員,以查看可能使用工作的代碼的其他方面。