批量if语句多个输入

时间:2016-01-11 13:33:39

标签: batch-file if-statement

所以我正在写一个有趣的批处理文件,我有一个多选语句,所以我有这样的:

@echo off
title John's apples
echo.
echo John got 3 apples, he gives 1 away. How many does he have then?
echo (Please answer with ONE capital letter, or type Exit to exit.)
echo =============
echo.
echo A) 2.5
echo B) 3.459475945
echo C) 2
echo D) 1
echo Exit
echo.
:answer
set /p ans=Your Answer:
if "%
if "%ans%"=="a" Echo Please answer with a capital letter!&set /p ans=Your Answer:
if "%ans%"=="b" Echo Please answer with a capital letter!&set /p ans=Your Answer:
if "%ans%"=="c" Echo Please answer with a capital letter!&set /p ans=Your  Answer:
if "%ans%"=="d" Echo Please answer with a capital letter!&set /p ans=Your Answer:
if "%ans%"=="A" Echo Wrong! The answer was 2.
if "%ans%"=="B" Echo Wrong! The answer was 2.
if "%ans%"=="C" Echo Right! Good job! The answer was 2.
if "%ans%"=="D" Echo Wrong! The answer was 2.
if "%ans%"=="Exit" exit

Pause
Exit

所以现在我想知道,如果有人给出了错误的答案(因为你必须输入一个大写字母,否则它将不起作用)它什么也没说。所以我想知道我是怎么做的: 如果A,B,C,D不是输入,请打印"请输入有效答案!"

有人可以帮忙吗?这可能已经被问到了,对不起......脚本中可能有一些随机的东西,因为我正在测试一些。

3 个答案:

答案 0 :(得分:0)

@echo off
title John's apples
echo.
echo John got 3 apples, he gives 1 away. How many does he have then?
echo (Please answer with ONE capital letter, or type Exit to exit.)
echo =============
echo.
echo A) 2.5
echo B) 3.459475945
echo C) 2
echo D) 1
echo Exit
echo.
:answer
set /p ans=Your Answer:
if "%ans%"=="a" Echo Please answer with a capital letter!&goto answer
if "%ans%"=="b" Echo Please answer with a capital letter!&goto answer
if "%ans%"=="c" Echo Please answer with a capital letter!&goto answer
if "%ans%"=="d" Echo Please answer with a capital letter!&goto answer
if "%ans%"=="A" Echo Wrong! The answer was 2.&goto end
if "%ans%"=="B" Echo Wrong! The answer was 2.&goto end
if "%ans%"=="C" Echo Right! Good job! The answer was 2.&goto end
if "%ans%"=="D" Echo Wrong! The answer was 2.&goto end
if "%ans%"=="Exit" exit
echo Please enter a valid answer!
goto answer

:end
Pause
Exit

答案 1 :(得分:0)

我猜测大写字母的分离是希望单独输出,而不是对'/ i'参数宽容。这是我的尝试。

@echo off
title John's apples

rem delayed expansion by preference.
setlocal enableDelayedExpansion

:init
cls
rem Added an initial label, and a clear for the 'ans' variable.
set "ans="
echo.
echo John got 3 apples, he gives 1 away. How many does he have then?
echo (Please answer with ONE capital letter, or type Exit to exit.)
echo =============
echo.
echo A) 2.5
echo B) 3.459475945
echo C) 2
echo D) 1
echo Exit
echo.
set /p ans=Your Answer:

rem Check if input is empty, then check for [a,b,c,d] in any case using the '/i' parameter,
rem Upon a valid letter, check for case. As well as a last check for a valid answer.
if not defined ans (
    echo Empty input . . . 
) else (
    if /i "!ans!" equ "exit" (
        exit /b
    ) else (
        if /i "!ans!" equ "A" (
            if "!ans!" equ "a" echo Please answer with a capital letter!
            echo Wrong! The answer was 2.
        ) else (
            if /i "!ans!" equ "B" (
                if "!ans!" equ "b" echo Please answer with a capital letter!
                echo Wrong! The answer was 2.
            ) else (
                if /i "!ans!" equ "C" (
                if "!ans!" equ "c" echo Please answer with a capital letter!
                echo Right! Good job! The answer was 2.
                ) else (
                    if /i "!ans!" equ "D" (
                        if "!ans!" equ "d" echo Please answer with a capital letter!
                        echo Wrong! The answer was 2.
                    ) else (
                        echo Invalid input . . .
                    )
                )
            )
        )
    )
)
pause
goto :init

答案 2 :(得分:0)

我可能稍微改了一下,但效果更好:

@echo off
color 0a
cls
:game
title John's apples
cls
echo John got 3 apples, he gives 1 away. How many does he have then?
echo (Please answer with ONE capital letter)
echo ===============================================================
echo A) 2.5
echo B) 3.459475945
echo C) 2
echo D) 1
echo E) Exit
set /p ans=

if %ans%== a goto capital
if %ans%== A goto wrong
if %ans%== b goto capital
if %ans%== B goto wrong
if %ans%== c goto capital
if %ans%== C goto correct
if %ans%== d goto capital
if %ans%== D goto wrong
if %ans%== e exit

:wrong
Echo Wrong! The Answer was 2.
pause
exit

:capital
echo Please answer with a capital letter!
pause 
goto game

:correct
echo Right! Good job! The answer was 2!
pause
exit