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