我有一个任务,需要我从用户请求数组大小,然后请求数字来填充数组。该计划仅打印每个循环的唯一编号。它还会通知用户他们输入的号码是否重复。我已经完成了这个工作,它应该如此。导师随后发布了一个教程视频,介绍如何编写它。这段代码与我的完全不同,我试图重写它来理解她的逻辑。我无法按照教程显示的那样工作,而且我也不理解她所包含的一些内容。有人可以看看这个,并帮助我了解她正在尝试做什么,如果它按照书面方式工作?与教练苦苦挣扎教程
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 –