我需要检查目录是否为空.问题是,我想将目录视为空,如果它包含子文件夹,无论子文件夹是否包含文件.我只关心我正在看的路径中的文件.这个目录将通过网络访问,这种情况有点复杂.最好的方法是什么?
好吧,我想做的事情似乎相当简单,但它并没有按照我想要的方式工作.我知道我只是没有得到什么.本质上我试图读取控制台输入,将其分配给变量.然后我想检查该变量以查看它是否是有效数字.如果不是,我想告诉用户它无效并重新开始循环,直到我得到一个有效的数字,然后退出.这是我的代码,请你帮我理解我做错了什么?
const int AVERAGE_IQ = 100;
int userIQ;
bool done = false;
do
{
Console.Write("Please enter an IQ Score between 1 and 200: ");
userIQ = Convert.ToInt32(Console.ReadLine());
if (userIQ == 0 || userIQ >= 200)
{
Console.WriteLine("You have entered an invalid IQ Score, please try again.");
done = false;
}
else if (userIQ >= AVERAGE_IQ)
{
Console.WriteLine("{0} is an above average IQ.", userIQ);
done = true;
break;
}
else if (userIQ <= AVERAGE_IQ)
{
Console.WriteLine("{0} is an below average IQ.", …Run Code Online (Sandbox Code Playgroud) 我需要知道如何在C#中动态调整数组大小.在下面我写的方法中,我需要能够返回一个只包含用户输入的数字的数组,最多包含8个数字.因此,如果用户决定他们只想输入3个数字,则该数组应该只包含3个数字,而不是8个.
现在我知道数组在实例化时需要包含一个大小.那么如何在不使用列表的情况下解决这个问题呢?循环完成后有没有办法重新调整数组的大小?
提前致谢.
static int[] fillArray()
{
int[] myArray;
myArray = new int[8];
int count = 0;
do
{
Console.Write("Please enter a number to add to the array or \"x\" to stop: ");
string consoleInput = Console.ReadLine();
if (consoleInput == "x")
{
Array.Resize(ref myArray, count);
return myArray;
}
else
{
myArray[count] = Convert.ToInt32(consoleInput);
++count;
}
} while (count < 8);
Array.Resize(ref myArray, count);
return myArray;
}
Run Code Online (Sandbox Code Playgroud) 我尝试使用以下方法引用已插入到数组中的对象的属性时遇到问题.
public static void AddEmployees()
{
string empID;
decimal empWage;
int count = 0;
do
{
Console.Write("Please enter the employee ID number: ");
empID = Convert.ToString(Console.ReadLine());
Console.Write("Please enter the employee wage: ");
empWage = Convert.ToDecimal(Console.ReadLine());
var employeeObj = CreateEmployee(empID, empWage);
employeeArray[count] = employeeObj;
++count;
} while (count < 6);
}
Run Code Online (Sandbox Code Playgroud)
我想以某种可读的格式打印出这个数组中的信息,但我不知道如何引用empWage或empID.理想情况下,我想使用某种foreach循环,如下所示:
public static void DisplayEmployees()
{
foreach (var obj in employeeArray)
{
Console.WriteLine("Employee ID: {0}", empID);
Console.WriteLine("Employee Wage: {0}", empWage);
Console.WriteLine();
}
}
Run Code Online (Sandbox Code Playgroud)