简体   繁体   中英

How do i increase a number by 1 in every line that contain the number 1

i want it to increase numbers on each number "1" but also repeat the increase two times you can look at my example down below i am trying to delete a bunch of files in my program as i said look down below but if you know how to Directory.Delete(path); without the if (Directory.Exists) and all that it would also be great. cause i rather just type Directory.Delete(path) and if it dont find the folder it will just continue. Sorry for my bad English

Everything google sucks

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!");
}

to

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 ^^

You can write a function to delete directory,

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

Now iterate n times to get your expected result, here n is 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");
} 

You want to loop over the array with an iterator variable. Initialize i with your starting point and length to your exclusive ending i (in your example, it's 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!");
    }
}

@pr0f3ss answer worked after he helped me on discord so final answer is

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!");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM