Batch File input validation - Make sure user entered an integer

前端 未结 16 2359
抹茶落季
抹茶落季 2020-12-03 14:33

I\'m experimenting with a Windows batch file to perform a simple operation which requires the user to enter a non-negative integer. I\'m using simple batch-file techniques t

16条回答
  •  既然无缘
    2020-12-03 15:20

    :ASK
    SET /P number= Choose a number [1 or 2]: 
    IF %number% EQU 1 GOTO ONE
    IF %number% NEQ 1 (
      IF %number% EQU 2 GOTO TWO
      IF %number% NEQ 2 (
        CLS
        ECHO You need to choose a NUMBER: 1 OR 2.
        ECHO.
        GOTO ASK
      )
    )
    

    It works fine to me. If he chooses numbers less or greater, strings, floating number etc, he wil receive a message ("You need to choose a NUMBER: 1 OR 2.") and the INPUT will be asked again.

提交回复
热议问题