2016-01-11 44 views
0

所以我在寫一個有趣的批處理文件,我有一個多項選擇語句,所以我有這樣的:批if語句多個輸入

@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不是輸入,打印「請輸入一個有效的答案!」

任何人都可以幫忙嗎?這可能已經被問到了,對此抱歉......腳本中可能會有一些隨機的東西,因爲我正在測試一些東西。

+0

你可以在一個循環的每個問題。所以雖然答案不正確,但再次詢問 – miqdadamirali

+0

@miqdadamirali是的,但我想讓他們證明這是一個無效的答案,你知道它並不是真的必要的,因爲大多數人並不那麼愚蠢,但我想提高我對編碼的認識XD –

+0

你可以做的是,當輸入答案時,驗證它。如果無效,請輸入錯誤消息(再次詢問是否需要),然後繼續結束/下一個問題 – miqdadamirali

回答

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 
+0

,這對我來說最合適。謝謝!要記住這一個:) –

+0

好吧,如果你認爲這個答案有幫助你,請點擊它的綠色複選標記,然後當你有足夠的代表這樣做,upvote它 – Aacini

+0

@JeroenSteens:那麼? – Aacini

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 
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