Batch if statements not working

前端 未结 1 631
天涯浪人
天涯浪人 2021-01-03 11:30

The following code is not working. Whatever I input just returns an error, and then goes back to Retry.

@echo off

:maths
set /p Mathsa=\"first          


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 11:44

    It's the quotes around your Mathso variable. Change them all to use quotes around both sides of the equality, such as with:

    if "%Mathso%" == "+" GOTO run
    

    and they should work better.

    The reason for this is that cmd is not quite the same as UNIXy shells. The quotes are preserved on the left hand of the equality so that what you end up with is:

    if "+" == + then ...
    

    and "+" is not equal to +.

    By putting quotes on both sides, it becomes:

    if "+" == "+" then ...
    

    which is true.


    And, as Aacini points out in the comment, your calculation set uses the wrong variable for the operator. You should change:

    set /a Mathsans=%Mathsa%%Mathsp%%Mathsb%
    

    into:

    set /a Mathsans=%Mathsa%%Mathso%%Mathsb%
                                  ^
                                  |
                                  see here
    

    0 讨论(0)
提交回复
热议问题