我想制作一个从用户输入运行jar X次的批处理文件。我已经找了如何处理用户输入,但我不完全确定。 在这个循环中,我想增加我发送给jar的参数。
截至目前,我不知道
所以,当我从命令行运行这个小蝙蝠文件时,我能够进行用户输入,但是一旦进入for循环,就会吐出“命令的语法不正确
到目前为止,我有以下
@echo off
echo Welcome, this will run Lab1.jar
echo Please enter how many times to run the program
:: Set the amount of times to run from user input
set /P numToRun = prompt
set numParam = 10000
set strParam = 10000
:: Start looping here while increasing the jar pars
:: Loop from 0 to numToRun
for /L %%i in (1 1 %numToRun%) do (
java -jar Lab1.jar %numParam% %strParam%
)
pause
@echo on
任何建议都会有所帮助
修改 随着最近的变化,它似乎没有运行我的jar文件。或者至少似乎没有运行我的测试回声程序。似乎我的用户输入变量没有设置为我输入的,它保持在0
答案 0 :(得分:3)
如果您阅读文档(从命令行键入help for
或for /?
),那么您将看到执行FOR循环固定次数的正确语法。
for /L %%i in (1 1 %numToRun%) do java -jar Lab1.jar %numParam% %strParam%
如果要使用多行,则必须使用行继续
for /L %%i in (1 1 %numToRun%) do ^
java -jar Lab1.jar %numParam% %strParam%
或括号
for /L %%i in (1 1 %numToRun%) do (
java -jar Lab1.jar %numParam% %strParam%
REM parentheses are more convenient for multiple commands within the loop
)
答案 1 :(得分:1)
我上次发生的问题是变量扩展的原因。这实际上是在dreamincode.net上回答的:Here
最终代码:
@echo off
echo Welcome, this will run Lab1.jar
:: Set the amount of times to run from user input
set /P numToRun= Please enter how many times to run the program:
set /a numParam = 1000
set /a strParam = 1000
setlocal enabledelayedexpansion enableextensions
:: Start looping here while increasing the jar pars
:: Loop from 0 to numToRun
for /L %%i in (1 1 %numToRun%) do (
set /a numParam = !numParam! * 2
set /a strParam = !strParam! * 2
java -jar Lab1.jar !numParam! !strParam!
:: The two lines below are used for testing
echo %numParam% !numParam!
echo %strParam% !strParam!
)
@echo on