or
演算子を実装するために、ネストされたif-elseステートメントを使用しようとしています。
問題は、コードが最後のネストされた以外のステートメントの外側でしか機能しないということです。理由はわかりません。ELSEブランチの数学的演算と環境変数の出力が期待どおりに動作しないのはなぜですか?
// と記されているノートを実際にスクリプトに記載していますが、私がしようとしていることの手がかりを得るのに役立ちます。ここで
は私のバッチスクリプトです:
:computerMakePick
setLocal
set /a currentNumber= 15
set /a addOne=%currentNumber%+1
set /a addTwo=%currentNumber%+2
//the next segment implements OR operator for two conditions using nested if-else statement
if %addOne% == 7 ( //checking first condition.
echo Computer chose: %addOne%
set /a currentNumber= %addOne%
)else (
if %addTwo% == 8 (// now checking: OR second condition
echo Computer chose: %addTwo%
set /a currentNumber= %addTwo%
)else (// if not both of the above then do this. NOW this section below doesn't work
set /a bottomlimit= 1
set /a upperlimit= 2
set /a limit=%upperlimit%-%bottomlimit%+1
set /a randomChoice= %bottomlimit% + %RANDOM% %% %limit%
set /a currentNumber= %currentNumber%+%randomChoice%
echo Computer chose: %currentNumber%
)
)
endLocal & set /a currentNumber= %currentNumber%
goto :eof
私は以下のように外部への最後のelse
セクションを取る場合、それは動作します:
:computerMakePick
setLocal
set /a currentNumber= 15
set /a addOne=%currentNumber%+1
set /a addTwo=%currentNumber%+2
//the next segment implements OR operator for two conditions using nested if-else statement
if %addOne% == 7 ( //checking first condition.
echo Computer chose: %addOne%
set /a currentNumber= %addOne%
)else (
if %addTwo% == 8 (// now checking: OR second condition
echo Computer chose: %addTwo%
set /a currentNumber= %addTwo%
)else (
echo. // need to put something in here or else it doesn't work.
) // could also delete this last else-statment but it doesn't matter
)
//now this below works fine. and I don't understand why under second-else section it doesn't
set /a bottomlimit= 1
set /a upperlimit= 2
set /a limit=%upperlimit%-%bottomlimit%+1
set /a randomChoice= %bottomlimit% + %RANDOM% %% %limit%
set /a currentNumber= %currentNumber%+%randomChoice%
echo Computer chose: %currentNumber%
endLocal & set /a currentNumber= %currentNumber%
goto :eof
私ならば、私は意味働いていないと言ってそれぞれの変数の値を出力します。bottomlimit
、upperlimit
、limit
など、2番目のelseステートメント内で定義されている場合、たとえばコマンドラインの場合echo value of limit is = %limit%
空白(何もありません)が表示されます。
なぜこのようなことが起こり、どのようにして2番目の内部で動作するように修正できますかelse声明?
の答えを読んで答えを読む(HTTPS [ '%は=任意の場所でこのように書く=%']あなたができるコメント:/ /stackoverflow.com/a/12408045/995714)、または単に ''行末にコメントを残す ''を使用してください。 '&::行末までコメント'も動作します。](https://superuser.com/a/82244/241386)。そうすれば、コードを試すには '//'行を削除する必要はありません –