2016-07-16 48 views
-2

我有一個任務,需要我從用戶請求數組大小,然後請求數字來填充數組。該計劃僅打印每個循環的唯一編號。它還會通知用戶他們輸入的號碼是否重複。我已經完成了這個工作,它應該如此。導師隨後發佈了一個教程視頻,介紹如何編寫它。這段代碼與我的完全不同,我試圖重寫它來理解她的邏輯。我無法按照教程顯示的那樣工作,而且我也不理解她所包含的一些內容。有人可以看看這個,並幫助我瞭解她正在嘗試做什麼,如果它按照書面方式工作?與教練苦苦掙扎教程

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace DuplicateHandsOn 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //create an array of type int 
      int[] aList; 

      //create a counter to keep track of how many numbers have been entered 
      int counter = 0; 

      //create a boolean flag to let us know whether the number can be added or not 
      bool isDuplicate = false; 

      //ask the user how many numbers they will be entering 
      Console.WriteLine("How many numbers will you enter?"); 
      int arraySize = Convert.ToInt32(Console.ReadLine()); 

      //initialize the array with that amount 
      aList = new int[arraySize]; 

      while (counter < arraySize) 
      { 
       //prompt the user for the first number 
       Console.Write("Enter Number: "); 
       int num1 = Convert.ToInt32(Console.ReadLine()); 

       //check if the number is between 10 and 100 
       if (num1 >= 10 || num1 <= 100) 
       { 
        //check if this number exists in the array 
        for (int i = 0; i < aList.Length; i++) 
        { 
         if (aList[i] == num1) 
         { 
          //this number exists in the list 
          Console.WriteLine("{0} has already been entered", aList[i]); 
          isDuplicate = true; 
         } 
        } 

        if (isDuplicate) 
        { 
         //put the number into the array 
         aList[counter] = num1; 
        } 

        //print the array 
        for (int j = 0; j < aList.Length; j++) 
        { 
         //exclude zeros 
         if (aList[j] == 0) 
         { 
          continue; 
         } 
         else 
         { 
          Console.WriteLine(aList[j]); 
         } 
        } 

        //increment the counter 
        counter++; 

        //reset the flag 
        isDuplicate = false; 
       } 
       else 
       { 
        Console.WriteLine("Numbers should be between 10 and 100"); 
       } 
      } 

      #if DEBUG 
      Console.ReadKey(); 
      #endif 

     } 
    } 
} 
+0

它並沒有爲我使用CSC.EXE C#編譯器在.NET –

回答

1

代碼中存在三個錯誤,以及很多樣式問題,但是這些錯誤很容易指出。我添加了// FIX評論來顯示我改變的內容,希望它有道理。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace DuplicateHandsOn 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //create an array of type int 
      int[] aList; 

      //create a counter to keep track of how many numbers have been entered 
      int counter = 0; 

      //create a boolean flag to let us know whether the number can be added or not 
      bool isDuplicate = false; 

      //ask the user how many numbers they will be entering 
      Console.WriteLine("How many numbers will you enter?"); 
      int arraySize = Convert.ToInt32(Console.ReadLine()); 

      //initialize the array with that amount 
      aList = new int[arraySize]; 

      while (counter < arraySize) 
      { 
       //prompt the user for the first number 
       Console.Write("Enter Number: "); 
       int num1 = Convert.ToInt32(Console.ReadLine()); 

       // FIX: This should be && instead of || to test if 
       // both of these conditions are true to match 
       // the comment 
       //check if the number is between 10 and 100 
       if (num1 >= 10 && num1 <= 100) 
       { 
        //check if this number exists in the array 
        for (int i = 0; i < aList.Length; i++) 
        { 
         if (aList[i] == num1) 
         { 
          //this number exists in the list 
          Console.WriteLine("{0} has already been entered", aList[i]); 
          isDuplicate = true; 
         } 
        } 

        // FIX: This should only happen if the number 
        // is not a duplicate 
        if (!isDuplicate) 
        { 
         //put the number into the array 
         aList[counter] = num1; 

         // FIX: Move this line into here to only increment 
         // the counter if th enumber is placed in the array 
         //increment the counter 
         counter++; 
        } 

        //print the array 
        for (int j = 0; j < aList.Length; j++) 
        { 
         //exclude zeros 
         if (aList[j] == 0) 
         { 
          continue; 
         } 
         else 
         { 
          Console.WriteLine(aList[j]); 
         } 
        } 

        //reset the flag 
        isDuplicate = false; 
       } 
       else 
       { 
        Console.WriteLine("Numbers should be between 10 and 100"); 
       } 
      } 

#if DEBUG 
      Console.ReadKey(); 
#endif 

     } 
    } 
} 
+0

你剛纔救了我從寫這個答案的工作:D還我會用'counter',而不是'aList.Length'爲兩個循環。在這種情況下,*排除零* if語句將不是必要的......如果這段代碼是由C#教師編寫的,我會感到非常震驚! –

+0

我有一個鏈接到視頻:)。這裏也是我對一些電子郵件的迴應摘錄,當時我質疑了一些材料並解釋了我所做的一些改變。 「這聽起來像你只是無意中修改了陳述,因爲我們想問」這個數字在10到100之間嗎?「,這絕不會是一個AND語句,我們知道這將是一個雙重條件或 如果我們考慮這個問題,由於我們不希望數字低於10,因此聲明必須> = 10。同樣,我們需要設置<= 100的上限,因爲我們希望數字不超過100。 「 – Mike

+0

可能是獲得退款的時間。該教師不理解基本的布爾邏輯,這對於課程來說不是一個好兆頭。祝你好運。 –

相關問題