對不起,但我得到的錯誤,可能包含我所有的代碼。批處理文件額外迭代到只有一個標籤
我做了兩個文件,一個(rngInputTest)啓動並檢查執行循環,並通過call命令中的參數將四個變量傳遞給第二個(rngCompare)文件。
第二個文件然後將這四個參數保存到rngCompare的局部四個變量中,然後使用%random%兩次。之後,它將兩個新變量(rng1和rng2)設置爲比較var,並將其傳遞迴rngInputTest,並將其保存到var結果中。
rngInputTest然後做了最後的檢查,如果checkLoop var是2,它會轉到最終的標籤/函數(rngInputTestDisplay),它的工作原理是,它回聲良好。
我遇到的問題是,沒有我的解決方法,在rngInputTestDisplay運行一次之後第二次執行到rngInputTestDisplay(沒有調用其他標籤/函數,並且第二次運行rngInputTestDisplay時,它會清空結果VAR)
我目前的解決辦法是
IF %checkLoop%==%logicIII% goto :end
我把這個在rngInputTestDisplay的開始和停止額外的迭代。
沒有解決辦法,甚至把後:
echo in [label]
pause
在所有的標籤/功能,rngInputTest和rngCompare
,我還是沒能找出額外的迭代是如何發生的,雖然我沒有注意執行循環只對rngInputTestDisplay執行。
這兩個文件的最終目標是創建一個可重複使用的隨機數,以便我可以多次調用第一個數據並將不同的數據集傳遞給它,然後將該數據返回給'main '文件。我知道我可以使用一次隨機%%和拯救自己的頭痛,但使用它一下後,它並沒有真正感覺到隨機
以下附上
兩個文件rngInputTest.bat
@ECHO off
::increment the loop-check var
set /a checkLoop+=1
goto :initLoopCheck
::used to check for looping
:initLoopCheck
::before execution loops to rngCompare
set logicI=1
::after execution loops back from rngCompare
set logicII=2
::used in rngInputTestDisplay,phantom loop
set logicIII=3
::starts the initial loop
IF %checkLoop%==%logicI% goto :rngInputTestInitVarSet
::checks if code has looped back from rngCompare.bat
IF %checkLoop%==%logicII% goto :logic
::sets all the non-increment variables
:rngInputTestInitVarSet
::min-max's for use in rngCompare.bat :randomGen
set minI=0
set maxI=50
set minII=0
set maxII=50
goto :logic
::checks to see how far checkLoop has incremented
:logic
IF %checkLoop%==%logicI% goto :rngInputTestPass
IF %checkLoop%==%logicII% goto :rngInputTestDisplay
::passes min-max's to rngCompare via parameters
:rngInputTestPass
set checkLoop+=1
call rngCompare.bat %minI% %minII% %maxI% %maxII%
goto :rngInputTestDisplay
:rngInputTestDisplay
::this IF is important
::without it the code would loop..only to
:: :rngInputTestDisplay
::which causes result to be empty
IF %checkLoop%==%logicIII% goto :end
set /a checkLoop+=1
set result=%~1
echo result:%result%
pause
:end
rngCompare.bat
@ECHO off
goto :rngCompareInitVarSet
::initial variable set
:rngCompareInitVarSet
set MaxI=0
set MaxII=0
set MinI=0
set MinII=0
set result=0
goto :afterPass
::after rngInputTestPass
:afterPass
set MinI=%~1
set MinII=%~2
set MaxI=%~3
set MaxII=%~4
goto :randomGen
::generate two random numbers
:randomGen
::gets two random numbers from the variables passed from rngInputTest.bat
set /a rngI=(%RANDOM%*%MaxI%/32768)+%MinI%
set /a rngII=(%RANDOM%*%MaxII%/32768)+%MinII%
goto :compareRNG
::add the two rng numbers
:compareRNG
::adds the two numbers together
set /a compare=%rngI%+%rngII%
goto :passToRNGInputTest
::pass the compare var back to rngInputTest
:passToRNGInputTest
call rngInputTest.bat %compare%
呃。你有遞歸(A調用B調用A)。但我不認爲這是你想要的。 「rngCompare.bat」shoud * return *(EXIT/B)返回調用者「rngInputTest.bat」。它應該*不*遞歸調用「rngInputTest.bat」。 – dbenham