0
我想製作一個批處理腳本來保存射箭中的分數。 當我運行它,並輸入分數1,2,3爲第1,第2和第3箭頭我的預期成果是:如何在批處理腳本中使用數組?
Scores:
1st arrow: 1
2nd arrow: 2
3rd arrow: 3
-
Total score: 6
Maximum possible score: 15
Accuracy: 40%
相反,我得到的輸出:
Scores:
1st arrow:
2nd arrow:
3rd arrow:
-
Total score: 6
Maximum possible score: 15
Accuracy:
代碼:
@echo off
color 0f
title Archery Score Logger
:askForConfig
set configFile=defaultConfig.cfg
cls
echo What config file would you like to use?
echo Leave blank for default config.
set /p configFile=Input:
if exist %configFile% (
goto loadConfig
) else (
echo Error!
echo The specified file does not exist.
pause
goto askForConfig
)
:loadConfig
(
set /p colour=
set /p numOfArrows=
set /p maxScorePerArrow=
)<%configFile%
color %colour%
:mainMenu
cls
echo Main Menu
echo -
echo 1 to input scores
echo 2 to exit
echo -
set /p menuChoice=Input:
if %menuChoice% == 1 (
set arrowInputCount=1
set totalScore=0
set numOfXs=0
cls
goto inputScores
) else if %menuChoice% == 2 (
exit
)
:inputScores
cls
if %arrowInputCount% gtr %numOfArrows% (
echo Done!
pause
goto output
)
set suffix=th
if %arrowInputCount% equ 1 (
set suffix=st
) else if %arrowInputCount% equ 2 (
set suffix=nd
) else if %arrowInputCount% equ 3 (
set suffix=rd
)
echo Input the score for the %arrowInputCount%%suffix% arrow ^(%numOfArrows% total arrows^).
echo Range: ^(1-%maxScorePerArrow%^), 'm' for miss, 'x' for an x.
set /p arrow[arrowInputCount]=Input:
if %arrow[arrowInputCount]% geq 1 (
if %arrow[arrowInputCount]% leq %maxScorePerArrow% (
set /a totalScore+=%arrow[arrowInputCount]%
set /a arrowInputCount+=1
goto inputScores
)
) else if %arrow[arrowInputCount]% == m (
set /a arrowInputCount+=1
goto inputScores
) else if %arrow[arrowInputCount]% == x (
set /a totalScore+=%maxScorePerArrow%
set /a arrowInputCount+=1
goto inputScores
)
echo Error!
echo The value you entered was invalid.
pause
goto inputScores
:output
set /a maxPossibleScore=%numOfArrows%*%maxScorePerArrow%
set /a accuracy=%totalScore%/(%maxPossibleScore%/100)
set outputCount=1
cls
echo Scores:
:outputLoop
set suffix=th
if %outputCount% equ 1 (
set suffix=st
) else if %outputCount% equ 2 (
set suffix=nd
) else if %outputCount% equ 3 (
set suffix=rd
)
if %outputCount% leq %numOfArrows% (
echo %outputCount%%suffix% arrow: %arrow[outputCount]%
set /a outputCount+=1
goto outputLoop
)
echo -
echo Total score: %totalScore%
echo Maximum possible score: %maxPossibleScore%
echo Accuracy: %accuracy%^%
echo -
pause
exit
我認爲問題是與數組,但讀了他們之後,我仍然看不到我的代碼有問題。 我看到他們固定它通過添加一條線,上面寫着幾個類似的問題:
setlocal EnableDelayedExpansion
,但我試過之後@echo在程序開始加入其關閉,它什麼也沒有改變。
0f
3
5
所有數組管理細節都在[數組,鏈表和其他數據結構中的cmd.exe(批處理)腳本]中進行了詳細說明(https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-其他數據結構 - 在-CMD-EXE分批腳本/ 10167990#10167990) – Aacini