如何在包含数字1的每一行中将数​​字增加1

时间:2019-08-17 12:56:45

标签: c# notepad++

我希望它在每个数字“ 1”上增加数字,但也要重复两次,您可以在下面的示例中查看我正在尝试删除程序中的一堆文件,因为我说的是在下面查看你知道如何Directory.Delete(path);如果没有if(Directory.Exists),那么一切也将很棒。因为我宁愿只是键入Directory.Delete(path),如果找不到文件夹,它将继续。对不起,我的英语不好

谷歌的一切都糟透了

if (Directory.Exists(delete[1]))
{
Directory.Delete(delete[1]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}
if (Directory.Exists(delete[1]))
{
Directory.Delete(delete[1]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}
if (Directory.Exists(delete[1]))
{
Directory.Delete(delete[1]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}
if (Directory.Exists(delete[1]))
{
Directory.Delete(delete[1]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}
if (Directory.Exists(delete[1]))
{
Directory.Delete(delete[1]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}

if (Directory.Exists(delete[1]))
{
Directory.Delete(delete[1]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}
if (Directory.Exists(delete[2]))
{
Directory.Delete(delete[2]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}
if (Directory.Exists(delete[3]))
{
Directory.Delete(delete[3]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}
if (Directory.Exists(delete[3]))
{
Directory.Delete(delete[3]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}
if (Directory.Exists(delete[4]))
{
Directory.Delete(delete[4]);
Console.WriteLine("You didnt fail");
}
else
{
Console.WriteLine("You failed!");
}

and so on

i showed what i expected over here ^^

3 个答案:

答案 0 :(得分:1)

您可以编写删除目录的功能,

public bool PurgeDirectory(string filePath)
{
   if (Directory.Exists(filePath))
   {
      Directory.Delete(filePath);
      return true;
   }
   return false;
}

现在迭代n次以获得您期望的结果,这里n为4

int n = 4;
string[] dirPaths = new string[n]{"dirPath1", "dirPath2"}; //string array will contain directory paths
for(int i = 0; i < n; i++)
{
  //You can call m times PurgeDirectory() function to avoid repeated code
  Console.WriteLine(PurgeDirectory(dirPaths[i]) ? "You Passed" : "You failed");
} 

答案 1 :(得分:1)

您要使用迭代器变量遍历数组。 用您的起点初始化i,并将length初始化为唯一的结尾i(在您的示例中为5)。

//gets the length of the array
int length = sizeof(delete)/sizeof(delete[0]); 
for(int i=0; i<length; i++){
    if (Directory.Exists(delete[i]))
    {
    Directory.Delete(delete[i]);
    Console.WriteLine("You didnt fail");
    }
    else
    {
    Console.WriteLine("You failed!");
    }
}

答案 2 :(得分:0)

@ pr0f3ss的答案在他帮助我解决不和谐问题后起作用,因此最终答案是

int length = 5; //5 since i had 5 lines
for(int i=0; i<length; i++){
    if (Directory.Exists(delete[i]))
    {
    Directory.Delete(delete[i]);
    Console.WriteLine("You didnt fail");
    }
    else
    {
    Console.WriteLine("You failed!");
    }