删除特殊字符和空格输入

问题描述 投票:0回答:1

我需要一种方法来在用户输入空格和特殊字符(例如,)时停止批量关闭。 !@#$%^&*()。顺便说一句,如果您想知道,我正在批量编写一个测验。

我一直在研究,大多数方法都使用“for”,比如“for /L %%i in (0.1.255) do”。我主要想要一个适用于多种事物的工具,因为这是一项测验并且有多个问题。

如果您想知道为什么某些变量是随机的,那是因为这是我唯一能想到的事情,而该变量并不那么重要。

我删除了底部的一些代码,以便我可以发布我的代码相当长的信息。当你在寻找的时候,你能告诉我为什么当我回答第 8 个问题时它会崩溃吗?我可能正在推动它,但是还有更好的方法来做我正在做的事情吗?

这是我的代码:

<!-- : BATCH
    cscript //nologo "%~f0?.wsf"
    @echo off
    title The Quiz of Minecraft
    color 2
    :menu
REM here is my title but it is quite big since its ASCII
    echo 1) Start
    echo 2) Guide
    echo 3) Exit
    set /p lollies=">> "
    if not defined lollies goto menu
    if %lollies% == 1 goto question1
    if %lollies% == 2 goto Guide
    if %lollies% == 3 (goto Exit) else (goto error1)
    set /p score=
    timeout /t 0 /nobreak >nul

    :error1
    cls
    echo Sorry but %lollies% was invalid, please try again!
    set lollies=
    pause
    goto menu

    :Guide
    cls
    echo Enter the number that is before your selection.  --^> 1) ^Start
    echo Would you like to continue? (y/n)
    set /p guider=">> "
    if not defined guider goto Guide
    if /i %guider% == y goto menu
    if /i %guider% == n (goto Guide) else (goto error2)

    :error2
    cls
    echo Sorry but %guider% was invalid, please try again!
    set guider=
    pause
    goto Guide

    :Exit
    cls
    echo Are you sure you want to leave? (y/n)
    set /p leaving=">> "
    if not defined leaving goto Exit
    if /i %leaving% == y goto bye
    if /i %leaving% == n (goto menu) else (goto error3)

    :error3
    cls
    echo Sorry but %leaving% was invalid, please try again!
    set leaving=
    pause
    goto Exit

    :bye
    cls
    echo Goodbye!
    timeout /t 1 /nobreak >nul
    exit

    :Name
    cls
    echo Please type your username below.
    set /p called="Enter Username: "
    if not defined called goto Name
    goto username

    :username
    cls
    echo Welcome %called%! Hope you enjoy!
    timeout /t 2 /nobreak >nul
    goto question1

    :question1
    cls
    echo 1: What year was Minecraft officially released?
    echo 1) 2008    2) 2009
    echo 3) 2010    4) 2011
    set /p answer1="Answer: "
    if not defined answer1 goto Start
    if %answer1% == 1 goto question2_1
    if %answer1% == 2 goto question2_1
    if %answer1% == 3 goto question2_1
    if %answer1% == 4 (goto question2) else (goto invalid1)

    :invalid1
    cls
    echo Sorry but %answer1% was invalid, please try again!
    set answer1=
    pause
    goto question1

    :question2
    set /a score+=1
    cls
    echo 2: What update added the elytra?
    echo 1) 1.16  2) 1.12
    echo 3) 1.9  4) 1.8
    set /p answer2="Answer: "
    if not defined answer2 goto question2
    if %answer2% == 1 goto question3_1
    if %answer2% == 2 goto question3_1
    if %answer2% == 3 goto question3
    if %answer2% == 2 (goto question3_1) else (goto invalid2)

    :invalid2
    cls
    echo Sorry but %answer2% was invalid, please try again!
    set answer2=
    pause
    goto question2

    :question2_1
    cls
    echo 2: What update added the elytra?
    echo 1) 1.16        2) 1.12
    echo 3) 1.9     4) 1.8
    set /p answer2_1="Answer: "
    if not defined answer2_1 goto question2_1
    if %answer2_1% == 1 goto question3_1
    if %answer2_1% == 2 goto question3_1
    if %answer2_1% == 3 goto question3
    if %answer2_1% == 2 (goto question3_1) else (goto invalid2_1)

    :invalid2_1
    cls
    echo Sorry but %answer2_1% was invalid, please try again!
    set answer2_1=
    pause
    goto question2_1

    :question3
    set /a score+=1
    cls
    echo 3: What update was the combat update?
    echo 1) 1.9  2) 1.2
    echo 3) 1.15  4) 1.13
    set /p answer3="Answer: "
    if not defined answer3 goto question3
    if %answer3% == 1 goto question4
    if %answer3% == 2 goto question4_1
    if %answer3% == 3 goto question4_1
    if %answer3% == 4 (goto question4_1) else (goto invalid3)

    :invalid3
    cls
    echo Sorry but %answer3% was invalid, please try again!
    set answer3=
    pause
    goto question3

    :question3_1
    cls
    echo 3: What update was the combat update?
    echo 1) 1.9     2) 1.2
    echo 3) 1.15    4) 1.13
    set /p answer3_1="Answer: "
    if not defined answer3_1 goto question3_1
    if %answer3_1% == 1 goto question4
    if %answer3_1% == 2 goto question4_1
    if %answer3_1% == 3 goto question4_1
    if %answer3_1% == 4 (goto question4_1) else (goto invalid3_1)

    :invalid3_1
    cls
    echo Sorry but %answer3_1% was invalid, please try again!
    set answer3_1=
    pause
    goto question3_1

    :question4
    cls
    echo 4: What mod makes torches work in the off-hand?
    echo 1) Off-hand Torches    2) Dynamic Lighting
    echo 3) Dynamic Lightning   4) Dynamic torches
    set /p answer4="ANswer: "
    if not defined answer4 goto question4
    if %answer4% == 1 goto question5_1
    if %answer4% == 2 goto question5
    if %answer4% == 3 goto question5_1
    if %answer4% == 4 (goto question5_1) else (goto invalid4)

    :invalid4
    cls
    echo Sorry but %answer4% was invalid, please try again!
    set answer4=
    pause
    goto question4

    :question4_1
    cls
    echo 4: What mod makes torches work in the off-hand?
    echo 1) Off-hand Torches    2) Dynamic Lighting
    echo 3) Dynamic Lightning   4) Dynamic torches
    set /p answer4_1="Answer: "
    if not defined answer4_1 goto question4_1
    if %answer4_1% == 1 goto question5_1
    if %answer4_1% == 2 goto question5
    if %answer4_1% == 3 goto question5_1
    if %answer4_1% == 4 (goto question5_1) else (goto invalid4_1)

    :invalid4_1
    cls
    echo Sorry but %answer4_1% was invalid, please try again!
    set answer4_1=
    pause
    goto question4_1

    :question5
    set /a score+=1
    cls
    echo 5: What are they making changes to in the 1.21 update?
    echo 1) The End      2) Villagers
    echo 3) The Nether  4) Combat
    set /p answer5="Answer: "
    if not defined answer5 goto question5
    if %answer5% == 1 goto question6_1
    if %answer5% == 2 goto question6_1
    if %answer5% == 3 goto question6_1
    if %answer5% == 4 (goto question6) else (goto invalid5)

    :invalid5
    cls
    echo Sorry but %answer5% was invalid, please try again!
    set answer5=
    pause
    goto question5

    :question5_1
    cls
    echo 5: What are they making changes to in the 1.21 update?
    echo 1) The End     2) Villagers
    echo 3) The Nether  4) Combat
    set /p answer5_1="Answer: "
    if not defined answer5_1 goto question5_1
    if %answer5_1% == 1 goto question6_1
    if %answer5_1% == 2 goto question6_1
    if %answer5_1% == 3 goto question6_1
    if %answer5_1% == 4 (goto question6) else (goto invalid5_1)

    :invalid5_1
    cls
    echo Sorry but %answer5_1% was invalid, please try again!
    set answer5_1="Answer: "
    pause
    goto question5_1

    :question6
    set /a score+=1
    cls
    echo 6: What mod adds realism to the elytra?
    echo 1) Better Elytra   2) Elytra Physics
    echo 3) Do A Barrel Roll    4) Elytra Remastered
    set /p answer6="Answer: "
    if not defined answer6 goto question6
    if %answer6% == 1 goto question7_1
    if %answer6% == 2 goto question7_1
    if %answer6% == 3 goto question7
    if %answer6% == 4 (goto question7_1) else (goto invalid6)

    :invalid6
    cls
    echo Sorry but %answer6% was invalid, please try again!
    set answer6=
    pause
    goto question6

    :question6_1
    cls
    echo 6: What mod adds realism to the elytra?
    echo 1) Better Elytra       2) Elytra Physics
    echo 3) Do A Barrel Roll    4) Elytra Remastered
    set /p answer6_1="Answer: "
    if not defined answer6_1 goto question6_1
    if %answer6_1% == 1 goto question7_1
    if %answer6_1% == 2 goto question7_1
    if %answer6_1% == 3 goto question7
    if %answer6_1% == 4 (goto question7_1) else (goto invalid6_1)

    :invalid6_1
    cls
    echo Sorry but %answer6_1% was invalid, please try again
    set answer6_1=
    pause
    goto question6_1

    :question7
    set /a score+=1
    cls
    echo 7: What name do you give an illager to make it attack any mob?
    echo 1) Johnny  2) Charlie
    echo 3) Jeff        4) Bob
    set /p answer7="Answer: "
    if not defined answer7 goto question7
    if %answer7% == 1 goto question8
    if %answer7% == 2 goto question8_1
    if %answer7% == 3 goto question8_1
    if %answer7% == 4 (goto question8_1) else (goto invalid7)

    :invalid7
    cls
    echo Sorry but %answer7% was invalid, please try again!
    set answer7=
    pause
    goto question7

    :question7_1
    cls
    echo 7: What name do you give an illager to make it attack any mob?
    echo 1) Johnny  2) Charlie
    echo 3) Jeff    4) Bob
    set /p answer7_1="Answer: "
    if not defined answer7_1 goto question7_1
    if %answer7_1% == 1 goto question8
    if %answer7_1% == 2 goto question8_1
    if %answer7_1% == 3 goto question8_1
    if %answer7_1% == 4 (goto question8_1) else (goto invalid7_1)

    :invalid7_1
    cls
    echo Sorry but %answer7_1% was invalid, please try again!
    set answer7_1=
    pause
    goto question7_1

    :question8
    set /a score+=1
    cls
    echo 8: What is the name for the 1.21 update?
    echo 1) The Bountiful   2) The Frostburn
    echo 3) Trails ^& tails 4) Forrests ^& Fossils
    set /p answer8="Answer: "
    if not defined answer8 goto question8
    if %answer8% == 1 goto question9_1
    if %answer8% == 2 goto question9_1
    if %answer8% == 3 goto question9_1
    if %answer8% == 4 goto (question9) else (goto invalid8)

    :invalid8
    cls
    echo Sorry but %answer8% was invalid, please try again!
    set answer8=
    pause
    goto question8

    :question8_1
    cls
    echo 8: What is the name for the 1.21 update?
    echo 1) The Bountiful   2) The Frostburn
    echo 3) Trails ^& tails 4) Forrests ^& Fossils
    set /p answer8_1="Answer: "
    if not defined answer8_1 goto question8_1
    if %answer8_1% == 1 goto question9_1
    if %answer8_1% == 2 goto question9_1
    if %answer8_1% == 3 goto question9_1
    if %answer8_1% == 4 goto (question9) else (goto invalid8_1)

    :invalid8_1
    cls
    echo Sorry but %answer8_1% was invalid, please try again!
    set answer8_1=
    pause
    goto question8_1

    :question9
    set /a score+=1
    cls
    echo 9: What was the first anarchy server on minecraft?
    echo 1) Arch Haven  2) Chaos Control
    echo 3) 2B2T        4) Nuke Nation
    set /p answer9="Answer: "
    if not defined answer9 goto question9
    if %answer9% == 1 goto question10_1
    if %answer9% == 2 goto question10_1
    if %answer9% == 3 goto question10
    if %answer9% == 4 goto (question10_1) else (goto invalid9)

    :invalid9
    cls
    echo Sorry but %answer9% was invalid, please try again!
    set answer9=
    pause
    goto question9

    :question9_1
    cls
    echo 9: What was the first anarchy server on minecraft?
    echo 1) Arch Haven  2) Chaos Control
    echo 3) 2B2T        4) Nuke Nation
    set /p answer9_1="Answer: "
    if not defined answer9_1 goto question9_1
    if %answer9_1% == 1 goto question10_1
    if %answer9_1% == 2 goto question10_1
    if %answer9_1% == 3 goto question10
    if %answer9_1% == 4 goto (question10_1) else (goto invaild9_1)

    :invalid9_1
    cls
    echo Sorry but %answer9_1% was invalid, please try again!
    set answer9_1=
    pause
    goto question9_1
BATCH : --->

<job><script language="VBScript">

Set WshShell = CreateObject ("WScript.Shell")
WshShell.SendKeys "{f11}"

 </script></job>
REM Review then score: 
batch-file
1个回答
0
投票

如果您多次重复代码的相同(或非常相似)部分,则必须在每个实例中维护它(这就是您最初的错误的来源:在问题 8 中您搞乱了语法)。最好合并代码,以便多次使用单个实例(函数)。

同样为了用户的安全输入,

choice
是要使用的命令(如注释中所建议的;它执行自己的错误处理并仅接受定义的输入)。

您有很多问题(缩短的脚本中有 9 个),所有问题都有四个可能的答案(相同的格式,这使得使用相同的子例程可以轻松处理所有问题并将所有问题收集在脚本中的一个位置,这使得维护代码并轻松添加其他问题非常容易。

<!-- : BATCH
rem @cscript //nologo "%~f0?.wsf"
@echo off
setlocal enabledelayedexpansion
title The Quiz of Minecraft
color 2
:menu
REM here is my title but it is quite big since its ASCII
echo 1) Start
echo 2) Guide
echo 3) Exit
choice /c 123
if errorlevel 3 goto exit
if errorlevel 2 goto guide
if errorlevel 1 goto game
set "score=0" Number of questions answered correct with first try
set "tries=0" Total number of answers given
timeout /t 0 /nobreak >nul

:Guide
cls
echo Enter the number that is before your selection.  --^> 1) Start
echo short description of the goal of this game
pause
goto menu

:Exit
cls
choice /M "Are you sure you want to leave?"
if errorlevel 2 goto menu
echo Goodbye!
timeout /t 1 /nobreak >nul
color
exit /b

:game
cls
echo Please type your username below.
set /p called="Enter Username: "
if not defined called goto game
cls
echo Welcome %called%! Hope you enjoy!
timeout /t 2 /nobreak >nul

for /f %%a in ('findstr /B "Q[1-9][0-9]*:" "%~f0"') do set /a "questions+=1"
for /l %%i in (1,1,%questions%) do call :askQuestion %%i
set /a percent=score*100/tries
echo Thank you for playing, %called%. Questions: %questions%. Tries:%tries%. Score:%percent%%% (first right:%score%)
timeout /t 5 >nul
color
goto :eof

:AskQuestion 
set "first="
:AskAgain
for /f "tokens=1-7 delims=:" %%a in ('findstr /b "Q%1:" "%~f0"') do (
  echo Question %1: %%b?  [DBG: %%g]
  echo 1^) %%c
  echo 2^) %%d
  echo 3^) %%e
  echo 4^) %%f
  choice /c 1234 /M "Your answer? "
  if !errorlevel! == %%g (
    set /a tries+=1
    if not defined first set /a score+=1 
    echo correct.
  ) else (
    set /a tries+=1
    set first=false 
    echo incorrect.
    goto :askAgain %1
  )
)
goto :eof

REM Q/A section. Format: Number:question:answer1:answer2:answer3:answer4:correctAnswer
Q1:What year was Minecraft officially released:2008:2009:2010:2011:4
Q2:What update added the elytra:1.16:1.12:1.9:1.8:3
Q3:What update was the combat update:1.9:1.2:1.15:1.13:4
Q4:What mod makes torches work in the off-hand:Off-hand torches:Dynamic Lighting:Dynamic Lightning:Dynamic torches:2
Q5:What are they making changes to in the 1.21 update:The End:villagers:The Nether:Combat:4
Q6:What mod adds realism to the elytra:Better Elytra:Elytra Physics:Do A Barrel Roll:Elytra Remastered:3
Q7:What name do you give an villager to make it attack any mob:Johnny:Charlie:Jeff:Bob:1
Q8:What is the name for the 1.21 update:The Bountiful:The Frostburn:Trails & tails:Forrests & Fossils:4
Q9:What was the first anarchy server on minecraft:Arch Haven:Chaos Control:2B2T:Nuke Nation:3

BATCH : --->
<job><script language="VBScript">
Set WshShell = CreateObject ("WScript.Shell")
WshShell.SendKeys "{f11}"
</script></job>
© www.soinside.com 2019 - 2024. All rights reserved.