计算两个变量的总和在间歇脚本 [英] Calculating the sum of two variables in a batch script

查看:164
本文介绍了计算两个变量的总和在间歇脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次堆栈溢出,所以请手下留情这个问题。我一直在尝试编程批次和使用DOSBox中给我的Linux机器上运行它们。

This is my first time on stack overflow so please be lenient with this question. I have been experimenting with programming with batch and using DOSbox to run them on my linux machine.

这是我一直在使用code:

Here is the code I have been using:

@echo off
set a=3
set b=4
set c=%a%+%b%
echo %c%
set d=%c%+1
echo %d%

,它的输出是:

3+4
3+4+1

我如何将添加两个变量,而不是附和该字符串的?

How would I add the two variables instead of echoing that string?

推荐答案

您需要使用属性 / A 在set命令。

You need to use the property /a on the set command.

例如,

set /a c=%a%+%b%

这允许您使用算术EX pressions中的set命令的,而不是简单的串联。

This allows you to use arithmetic expressions in the set command, rather than simple concatenation.

您code,那么将是:

Your code would then be:

@set a=3
@set b=4
@set /a c=%a%+%b%
echo %c%
@set /a d=%c%+1
echo %d%

和将输出:

7
8

这篇关于计算两个变量的总和在间歇脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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