【问题标题】:is there a way to get a function to repeat itself if a condition isnt met [duplicate]如果不满足条件,有没有办法让函数重复自身[重复]
【发布时间】:2013-05-10 14:20:17
【问题描述】:

我有这样的事情:

static int cantryagain=0;

private void myfunction(){

if (cantryagain==0)
{
    if(variableA=1)
        {
        //do my stuff
        //ta daaaa
        }
        else
        {
        //do something magical that will help make variableA=1 but 
        //if the magic doesnt work i only want it to try once.
        tryagain();
        }
    }
}

private void tryagain
{
    myfunction();
    cantryagain=1; //to make sure the magic only happens once, but 
            //obviously it never gets here as it does
            //myfunction again before it ever can... 
}

我知道这段代码非常糟糕。我对 c# 还很陌生。

我怎样才能正确地做出这样的事情?

【问题讨论】:

标签: c# if-statement


【解决方案1】:

你正在寻找一个循环

while(somethingNotMet){
    //do something
    somthingNotMet=false;
}

【讨论】:

【解决方案2】:

如果你真的想在不使用循环的情况下做到这一点,你可以使用可选参数并递归调用函数:

private void myfunction(int recursiveCount = 0)
{
    if (recursiveCount > 1)
    {
      // give up
      return;
    }

    if (variableA == 1)
    {
      //do my stuff
      //ta daaaa
    }
    else
    {
      myFunction(++recursiveCount);
    }
}

要使用它只需调用函数而不提供参数:

myfunction();

【讨论】:

  • 递归是一个高级主题,可能在涵盖基础知识之前不应该解决。
【解决方案3】:

如果你只想再试一次

static int cantryagain=0;

private void myfunction()
{
    for (int i = 0; i < 2; i++) // will loop a max of 2 times
    {
        if(variableA=1)
        {
            //do my stuff
            //ta daaaa
            break; //Breaks out of the for loop so you don't loop a second time
        }
        else if (i == 0)  // Don't bother if this isn't the first iteration
        {
            //do something magical that will help make variableA=1 but 
            //if the magic doesnt work i only want it to try once.
        }
    }
}

【讨论】:

  • 在尝试了以前的方法后,我自己想通了,但我的解决方案最接近这个答案并且效果很好。谢谢。
【解决方案4】:

是的,你想把函数放在一个循环中,并让函数返回一个布尔值,指示它是否应该运行

  private bool myFunction() {
    Random random = new Random();
    return random.Next(0, 100) % 2 == 0; // return true or false, this would be your logic to implement
  }

  public bool doSomething() {
    var tryAgain = false;
    do {
      tryAgain = myFunction(); // when myFunction returns false, the loop condition isn't met, and the loop will exit
    } while (tryAgain);
  }

【讨论】:

    【解决方案5】:

    您要查找的内容称为do while 循环!

    int attempts = 0;
    
    do
    {
         hasWorked = someFunction();
         attempts ++;
    }
    while(!hasWorked && attempts <= 1)
    

    虽然(大声笑)我在这里,但我想我会向您展示另一种类型的循环,称为For 循环。当您知道您希望代码运行多少次时,就会使用这种类型的循环。

    for(int x = 0; x < 10; x++)
    {
        Console.Writeline("Hello there number : " + X);
    }
    

    这将打印出来:

    Hello there number 0
    Hello there number 1
    Hello there number 2
    ...
    Hello there number 9
    

    【讨论】:

    • 将其设为do while 并丢失第一行。
    • 哇,这是一个愚蠢的错误。谢谢@AshBurlaczenko
    猜你喜欢
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2019-08-01
    • 2016-07-30
    相关资源
    最近更新 更多