批量if语句不工作 [英] Batch if statements not working

查看:184
本文介绍了批量if语句不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code不工作。无论我输入方法会返回一个错误,然后返回到重试

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 number? "
echo your first number is %Mathsa%

:retry

set /p Mathso="operator?(+-*/) "
if "%Mathso%" ==* GOTO run
if "%Mathso%" ==/ GOTO run
if "%Mathso%" ==+ GOTO run
if "%Mathso%" ==- GOTO run
echo Error, wrong operator. & goto retry

:run
set /p Mathsb="Second Number? "
set /a Mathsans=%Mathsa%%Mathsp%%Mathsb%
echo %Mathsans%
pause

我绝对不知道为什么它这样做。它是一所学校的项目和任何帮助,您可以给将AP preciated!

I have absolutely no idea why it's doing this. It's for a school project and any help you can give would be appreciated!

推荐答案

这是在你的 Mathso 变量引号。更改他们都周围使用引号的两个的平等的双方,如:

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.

这样做的原因是, CMD 是不太一样的UNIXy炮弹。该报价是pserved在平等的左手$ P $,使你最终是什么:

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

+是的的等于 +

通过将报价两侧,就变成:

By putting quotes on both sides, it becomes:

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

其中的的事实。

此外,由于Aacini在评论中指出,你的计算设置使用操作员错误的变量。您应该更改:

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%

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

这篇关于批量if语句不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆