2017-06-20 3 views
0

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 

私ならば、私は意味働いていないと言ってそれぞれの変数の値を出力します。bottomlimitupperlimitlimitなど、2番目のelseステートメント内で定義されている場合、たとえばコマンドラインの場合echo value of limit is = %limit%空白(何もありません)が表示されます。

なぜこのようなことが起こり、どのようにして2番目の内部で動作するように修正できますかelse声明?

+0

の答えを読んで答えを読む(HTTPS [ '%は=任意の場所でこのように書く=%']あなたができるコメント:/ /stackoverflow.com/a/12408045/995714)、または単に ''行末にコメントを残す ''を使用してください。 '&::行末までコメント'も動作します。](https://superuser.com/a/82244/241386)。そうすれば、コードを試すには '//'行を削除する必要はありません –

答えて

-1

else文内の変数は展開されません。 setlocal enabledelayedexpansion代わりに使用し、感嘆符を使用して、変数を表す:

:computerMakePick 
setLocal enabledelayedexpansion 
    set /a currentNumber= 15 
    set /a addOne=%currentNumber%+1 
    set /a addTwo=%currentNumber%+2 

    if %addOne% == 7 (
     echo Computer chose: %addOne% 
     set /a currentNumber= %addOne% 
    )else (
     if %addTwo% == 8 (
      echo Computer chose: %addTwo% 
      set /a currentNumber= %addTwo% 
     )else (
      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 
+1

いいえ、算術式内の変数をまったく展開しないでください。変数名にスペース文字やハイフン文字のような演算子が含まれている点を除いて、算術式には '%'と '!'のない変数名を使用します。 – Mofi

+0

@mofiそれはまだ 'else'ステートメントの中にあるので動作しません。それ以外の場合は、算術演算を拡張する必要はありません。 – Regejok

+3

うまく動作する私の答えを見てください。ヘルプからブロックを見てください。はい、 'echo computer selected:!currentNumber! 'の最後の行は遅延拡張が必要ですが、この行だけで、算術式を持つ他の行ではありません。そして、この__ECHO__行は、質問者の実際のコードにはほとんど存在しないでしょう。 – Mofi

2

は、次のコードを使用します。

@echo off 
:computerMakePick 
setLocal 

set "currentNumber=15" 
set /a addOne=currentNumber + 1 
set /a addTwo=currentNumber + 2 

rem // the next segment implements OR operator for two conditions using nested if-else statement 
if %addOne% == 7 (  rem // checking first condition. 
    echo Computer chose: %addOne% 
    set "currentNumber=%addOne%" 
) else if %addTwo% == 8 (rem // now checking: OR second condition 
    echo Computer chose: %addTwo% 
    set "currentNumber=%addTwo%" 
) else (rem // if not both of the above then do this. NOW this section below doesn't work 
    set "bottomlimit=1" 
    set "upperlimit=2" 
    set /a limit=upperlimit - bottomlimit + 1 
    set /a randomChoice=bottomlimit + %RANDOM% %% limit 
    set /a currentNumber+=randomChoice 
    setlocal EnableDelayedExpansion 
    echo Computer chose: !currentNumber! 
    endlocal 
) 

endLocal & set "currentNumber=%currentNumber%" 
goto :EOF 
  1. 環境変数は文字列型は常にあります。したがって、整数を使用する場合でも、数値は整数ではなく文字列としてメモリに格納されます。したがって、実際の理由がない場合は​​を使用しないでください。この結果、算術式の場合はnumberを文字列から整数に変換し、この算術式の結果を環境変数に割り当てるために整数から文字列に変換します。 set /a後の文字列である算術式内の環境変数膨張

  2. 使用は自動的にWindowsコマンドインタープリタとして通常ナンセンス各列が数字または電流値でなければならない環境変数の名前などのオペレータではないと解釈します式の評価のために整数に変換される。
    set /aコマンドラインがコマンドブロック内にある場合でも、算術式内で即時拡張または遅延展開が必要かどうか。

  3. そしてバッチファイルで//は、コマンドREMを使用すると、Windowsのコマンド・インタプリタは、最初のコマンドREMで行を解析して、何の構文エラーがない場合は、コマンドを実行することを考慮して、コメントはありませんが、参照%~ in REM statement。詳細については

  1. コマンドプロンプトウィンドウset /?で実行され、実際に出力ヘルプの全てを十分にお読みください。
    If you use any of the logical or modulus operators, you will need to 
    enclose the expression string in quotes. Any non-numeric strings in the 
    expression are treated as environment variable names whose values are 
    converted to numbers before using them. If an environment variable name 
    is specified but is not defined in the current environment, then a value 
    of zero is used. This allows you to do arithmetic with environment 
    variable values without having to type all those % signs to get their 
    values. 
    
  2. Why is no string output with 'echo %var%' after using 'set var = text' on command line?
  3. がインライン用IF ELSE syntax error within batch file?
関連する問題