简体   繁体   English

如果语句C#则结束else

[英]End a else if statement C#

I didn't know what else to use in this case but an if statement. 我不知道在这种情况下还需要使用什么,而是if语句。

What I am trying to do is I am getting a value string direction; 我正在尝试做的是获取值string direction; from a Windows form and checking if it has the value I am looking for. 从Windows窗体中检查是否具有我想要的值。 Then checking if the turtleDir which is a string value indicating the direction turtleDir has. 然后检查一下turtleDir是否为指示turtleDir方向的字符串值。

The problem comes here at else if statements, when the turtleDir is lookingleft it does all of the else if statements. 问题出在else if语句上,当turtleDir lookingleft看时,它会执行所有else if语句。 What I want it to do is after it has done that else if statement it needs to stop and wait for the next command. 我想要它做的是在完成之后,否则if语句需要停止并等待下一个命令。 not go through all the statements. 不要遍历所有陈述。

Can someone please advise me on how to fix it and what I am doing wrong? 有人可以建议我如何解决它以及我做错了什么吗?

Here is the code: 这是代码:

else if ( Program.form.direction == "right" ) 
{
   if ( turtleDir == "left" )
   {
      angle = -1.6f;
      turtleDir = "lookingLeft";
      Program.form.direction = "";
   }
   else if ( turtleDir == "lookingLeft" )
   {
      angle = 3.15f;
      turtleDir = "lookingDown";
   }
   else if ( turtleDir == "lookingDown" )
   {
      angle = 1.6f;
      turtleDir = "lookingRight";
   }
   else if ( turtleDir == "lookingRight" )
   {
      angle = 0.0f;
      turtleDir = "lookingUp";   
   }
}

You can use a switch statement on strings, too: 您也可以在字符串上使用switch语句:

switch (turtleDir) {
    case "left":
        angle = -1.6f;
        turtleDir = "lookingLeft";
        Program.form.direction = "";
        break;
    case "lookingLeft":
        angle = 3.15f;
        turtleDir = "lookingDown";
        break;
    // other cases
}

This way, the switch block is always exited after your instructions are done. 这样,在您完成指令后,总会退出switch块。 You can also specify what to do when the string matches none of these values by adding a case default: at the end. 您还可以通过在字符串末尾添加case default:值来指定当字符串不匹配这些值时的操作。 Remember each of these cases needs to be terminated by a break statement (or return / throw but i don't think you need those in this case). 请记住,每种情况都需要以break语句终止(或者return / throw但在这种情况下我不认为您需要这些)。

If still every case is executed, your problem lies somewhere else. 如果仍然执行每种情况,则您的问题出在其他地方。 If a method contains this code and is called starting with eg turtleDir == "left" , each successive call of the method will let turtleDir cycle until every case has been executed and turtleDir ends up with the final value "lookingUp" . 如果某个方法包含此代码并且以例如turtleDir == "left"开头的方式进行调用,则该方法的每次后续调用都将使turtleDir循环运行,直到执行turtleDir每种情况,并且turtleDir最终以最终值"lookingUp" So look at your control flow and maybe keep track whether you already performed this check. 因此,请查看您的控制流程,并可能跟踪您是否已经执行了此检查。 Maybe keep track of the elapsed time and change turtleDir only if it has been in a particular state for a while (i don't know your requirements). 也许只有当它处于特定状态一段时间(我不知道您的要求)时,才能跟踪经过的时间并更改turtleDir

EDIT: You should set Program.form.direction = "" in every case statement. 编辑:您应该在每个case语句中设置Program.form.direction = "" That's why your code gets executed over and over again. 这就是为什么您的代码会一遍又一遍地执行的原因。 Also, if no direction is entered, clear it, too. 另外,如果未输入方向,也请清除它。

You can use an empty return; 您可以使用空return; - Statement, but then you leave your method too... -声明,但随后您也离开了方法...

Use a switch 使用开关

switch (turtleDir)
{
    case "left":
        angle = -1.6f;
        turtleDir = "lookingLeft";
        Program.form.direction = "";
        break;

    case "lookingLeft":
        angle = 3.15f;
        turtleDir = "lookingDown";
        break;

    case "lookingDown":
        angle = 1.6f;
        turtleDir = "lookingRight";
        break;

    case "lookingRight":
        angle = 0.0f;
        turtleDir = "lookingUp";   
        break;

    default:
        // Optional, but place any actions if non of the above is matched
        break;
}

There is something else going on here.... 这里还有其他事情...

Only one branch of an IF/ELSE-IF statement will be executed. IF / ELSE-IF语句的仅一个分支将被执行。 You might want to refactor to a SWITCH statement for readability reasons; 出于可读性原因,您可能希望重构为SWITCH语句; but not because it would change your behaviour . 但这不是因为它会改变您的行为

Look at this code: 看下面的代码:

    if(0 == 0) { Console.WriteLine("Branch 1"); }
    else if(true==true) { Console.WriteLine("Branch 2"); }
    else if(false==false) { Console.WriteLine("Branch 3"); }

ALL of the conditions are true. 所有条件均成立。 And the first one executes. 然后第一个执行。 ONLY the first one. 只有第一个。

In this code - only the second two are true: if(0 == 2) { Console.WriteLine("Branch 1"); 在此代码中-仅后两个是正确的:if(0 == 2){Console.WriteLine(“ Branch 1”); } else if(true==true) { Console.WriteLine("Branch 2"); } else if(true == true){Console.WriteLine(“ Branch 2”); } else if(false==false) { Console.WriteLine("Branch 3"); } else if(false == false){Console.WriteLine(“ Branch 3”); } }

And ONLY the second one executes. 并且只有第二个执行。

A series of else-if's won't all execute if the condition is true. 如果条件为真,则一系列的else-if不会全部执行。

My guess is that you are looping through this really fast or something and the next iteration is entering the IF logic and executing the next branch. 我的猜测是,您正在快速快速地循环浏览,而下一次迭代将进入IF逻辑并执行下一个分支。 You might need to post more code so that you get more helpful answers for your particular situation. 您可能需要发布更多代码,以便针对您的特定情况获得更多有用的答案。

The best way to track this down, IMHO, is to just place a breakpoint on the start of your IF logic and step-through the code one execution at a time. 追踪这种情况的最佳方法,恕我直言,就是在IF逻辑的开始处放置一个断点,并一次逐步执行一个代码。 You should see that it only goes into one ELSE-IF branch and exits, and you can see if/why it re-enters. 您应该看到它仅进入一个ELSE-IF分支并退出,并且可以看到它是否/为什么重新进入。

Finally, the SWITCH statement doesn't allow you to implicitly execute multiple branches either. 最后,SWITCH语句也不允许您隐式执行多个分支。

    switch(test)
    {
        case "one":
            Console.WriteLine("Branch 1");
        case "two":
            Console.WriteLine("Branch 2");
    }

That won't even compile. 那甚至不会编译。 I believe in C/C++ you could fall though case statements unless you explicitly added break; 我相信在C / C ++中,除非明确添加了break,否则您可能会使用case语句。 or similar control statement. 或类似的控制声明。 In C# that's not the case. 在C#中并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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