我正在制作批处理文件编码器。用户逐字符输入密码,然后输出是该字符的简单代码。当我运行这个时,我收到的是每一个“真实”回显到输出。请帮忙!
@echo off
set /p num1=Character1:
set /p num2=Character2:
set /p num3=Character3:
set /p num4=Character4:
set /p num5=Character5:
set /p num6=Character6:
if %num1%==a goto true1
if %num1%==b goto true2
if %num1%==c goto true3
if %num1%==d goto true4
if %num1%==e goto true5
if %num1%==f goto true6
if %num1%==g goto true7
if %num1%==h goto true8
if %num1%==i goto true9
if %num1%==j goto true10
if %num1%==k goto true11
if %num1%==l goto true12
if %num1%==m goto true13
if %num1%==n goto true14
if %num1%==o goto true15
if %num1%==p goto true16
if %num1%==q goto true17
if %num1%==r goto true18
if %num1%==s goto true19
if %num1%==t goto true20
if %num1%==u goto true21
if %num1%==v goto true22
if %num1%==w goto true23
if %num1%==x goto true24
if %num1%==y goto true25
if %num1%==z goto true26
if %num2%==a goto true27
if %num2%==b goto true28
if %num2%==c goto true29
if %num2%==d goto true30
if %num2%==f goto true32
if %num2%==g goto true33
if %num2%==h goto true34
if %num2%==i goto true35
if %num2%==j goto true36
if %num2%==k goto true37
if %num2%==l goto true38
if %num2%==m goto true39
if %num2%==n goto true40
if %num2%==o goto true41
if %num2%==p goto true42
if %num2%==q goto true43
if %num2%==r goto true44
if %num2%==s goto true45
if %num2%==t goto true46
if %num2%==u goto true47
if %num2%==v goto true48
if %num2%==w goto true49
if %num2%==x goto true50
if %num2%==y goto true51
if %num2%==z goto true52
if %num3%==a goto true53
if %num3%==b goto true54
if %num3%==c goto true55
if %num3%==d goto true56
if %num3%==e goto true57
if %num3%==f goto true58
if %num3%==g goto true59
if %num3%==h goto true60
if %num3%==i goto true61
if %num3%==j goto true62
if %num3%==k goto true63
if %num3%==l goto true64
if %num3%==m goto true65
if %num3%==n goto true66
if %num3%==o goto true67
if %num3%==p goto true68
if %num3%==q goto true69
if %num3%==r goto true70
if %num3%==s goto true71
if %num3%==t goto true72
if %num3%==u goto true73
if %num3%==v goto true74
if %num3%==w goto true75
if %num3%==x goto true76
if %num3%==y goto true77
if %num3%==z goto true78
if %num4%==a goto true79
if %num4%==b goto true80
if %num4%==c goto true81
if %num4%==d goto true82
if %num4%==e goto true83
if %num4%==f goto true84
if %num4%==g goto true85
if %num4%==h goto true86
if %num4%==i goto true87
if %num4%==j goto true88
if %num4%==k goto true89
if %num4%==l goto true90
if %num4%==m goto true91
if %num4%==n goto true92
if %num4%==o goto true93
if %num4%==p goto true94
if %num4%==q goto true95
if %num4%==r goto true96
if %num4%==s goto true97
if %num4%==t goto true98
if %num4%==u goto true99
if %num4%==v goto true100
if %num4%==w goto true101
if %num4%==x goto true102
if %num4%==y goto true103
if %num4%==z goto true104
if %num5%==a goto true105
if %num5%==b goto true106
if %num5%==c goto true107
if %num5%==d goto true108
if %num5%==e goto true109
if %num5%==f goto true110
if %num5%==g goto true111
if %num5%==h goto true112
if %num5%==i goto true113
if %num5%==j goto true114
if %num5%==k goto true115
if %num5%==l goto true116
if %num5%==m goto true117
if %num5%==n goto true118
if %num5%==p goto true120
if %num5%==q goto true121
if %num5%==r goto true122
if %num5%==s goto true123
if %num5%==t goto true124
if %num5%==u goto true125
if %num5%==v goto true126
if %num5%==w goto true127
if %num5%==x goto true128
if %num5%==y goto true129
if %num5%==z goto true130
if %num6%==a goto true131
if %num6%==b goto true132
if %num6%==c goto true133
if %num6%==d goto true134
if %num6%==e goto true135
if %num6%==f goto true136
if %num6%==g goto true137
if %num6%==h goto true138
if %num6%==i goto true139
if %num6%==j goto true140
if %num6%==k goto true141
if %num6%==l goto true142
if %num6%==m goto true143
if %num6%==n goto true144
if %num6%==o goto true145
if %num6%==p goto true146
if %num6%==q goto true147
if %num6%==r goto true148
if %num6%==s goto true149
if %num6%==t goto true150
if %num6%==u goto true151
if %num6%==v goto true152
if %num6%==w goto true153
if %num6%==x goto true154
if %num6%==y goto true155
if %num6%==z goto true156
:true1
echo Cipher text- pl
:true2
echo Cipher text- vd
:true3
echo Cipher text- ed
:true4
echo Cipher text- jy
:true5
echo Cipher text- as
:true6
echo Cipher text- kv
:true7
echo Cipher text- 5g
:true8
echo Cipher text- j8
:true9
echo Cipher text- s2
:true10
echo Cipher text- sk
:true11
echo Cipher text- 34
:true12
echo Cipher text- gx
:true13
echo Cipher text- 7h
:true14
echo Cipher text- sr
:true15
echo Cipher text- mb
:true16
echo Cipher text- 6g
:true17
echo Cipher text- 8i
:true18
echo Cipher text- id
:true19
echo Cipher text- 28
:true20
echo Cipher text- s6
:true21
echo Cipher text- 4k
:true22
echo Cipher text- 6h
:true23
echo Cipher text- 3f
:true24
echo Cipher text- jf
:true25
echo Cipher text- 8d
:true26
echo Cipher text- 2s
:true27
echo Cipher text- pl
:true28
echo Cipher text- vd
:true29
echo Cipher text- ed
:true30
echo Cipher text- jy
:true31
echo Cipher text- as
:true32
echo Cipher text- kv
:true33
echo Cipher text- 5g
:true34
echo Cipher text- j8
:true35
echo Cipher text- s2
:true36
echo Cipher text- sk
:true37
echo Cipher text- 34
:true38
echo Cipher text- gx
:true39
echo Cipher text- 7h
:true40
echo Cipher text- sr
:true41
echo Cipher text- mb
:true42
echo Cipher text- 6g
:true43
echo Cipher text- 8i
:true44
echo Cipher text- id
:true45
echo Cipher text- 28
:true46
echo Cipher text- s6
:true47
echo Cipher text- 4k
:true48
echo Cipher text- 6h
:true49
echo Cipher text- 3f
:true50
echo Cipher text- jf
:true51
echo Cipher text- 8d
:true52
echo Cipher text- 2s
:true53
echo Cipher text- pl
:true54
echo Cipher text- vd
:true55
echo Cipher text- ed
:true56
echo Cipher text- jy
:true57
echo Cipher text- as
:true58
echo Cipher text- kv
:true59
echo Cipher text- 5g
:true60
echo Cipher text- j8
:true61
echo Cipher text- s2
:true62
echo Cipher text- sk
:true63
echo Cipher text- 34
:true64
echo Cipher text- gx
:true65
echo Cipher text- 7h
:true66
echo Cipher text- sr
:true67
echo Cipher text- mb
:true68
echo Cipher text- 6g
:true69
echo Cipher text- 8i
:true70
echo Cipher text- id
:true71
echo Cipher text- 28
:true72
echo Cipher text- s6
:true73
echo Cipher text- 4k
:true74
echo Cipher text- 6h
:true75
echo Cipher text- 3f
:true76
echo Cipher text- jf
:true77
echo Cipher text- 8d
:true78
echo Cipher text- 2s
:true79
echo Cipher text- pl
:true80
echo Cipher text- vd
:true81
echo Cipher text- ed
:true82
echo Cipher text- jy
:true83
echo Cipher text- as
:true84
echo Cipher text- kv
:true85
echo Cipher text- 5g
:true86
echo Cipher text- j8
:true87
echo Cipher text- s2
:true88
echo Cipher text- sk
:true89
echo Cipher text- 34
:true90
echo Cipher text- gx
:true91
echo Cipher text- 7h
:true92
echo Cipher text- sr
:true93
echo Cipher text- mb
:true94
echo Cipher text- 6g
:true95
echo Cipher text- 8i
:true96
echo Cipher text- id
:true97
echo Cipher text- 28
:true98
echo Cipher text- s6
:true99
echo Cipher text- 4k
:true100
echo Cipher text- 6h
:true101
echo Cipher text- 3f
:true102
echo Cipher text- jf
:true103
echo Cipher text- 8d
:true104
echo Cipher text- 2s
:true105
echo Cipher text- pl
:true106
echo Cipher text- vd
:true107
echo Cipher text- ed
:true108
echo Cipher text- jy
:true109
echo Cipher text- as
:true110
echo Cipher text- kv
:true111
echo Cipher text- 5g
:true112
echo Cipher text- j8
:true113
echo Cipher text- s2
:true114
echo Cipher text- sk
:true115
echo Cipher text- 34
:true116
echo Cipher text- gx
:true117
echo Cipher text- 7h
:true118
echo Cipher text- sr
:true119
echo Cipher text- mb
:true120
echo Cipher text- 6g
:true121
echo Cipher text- 8i
:true122
echo Cipher text- id
:true123
echo Cipher text- 28
:true124
echo Cipher text- s6
:true125
echo Cipher text- 4k
:true126
echo Cipher text- 6h
:true127
echo Cipher text- 3f
:true128
echo Cipher text- jf
:true129
echo Cipher text- 8d
:true130
echo Cipher text- 2s
:true131
echo Cipher text- pl
:true132
echo Cipher text- vd
:true133
echo Cipher text- ed
:true134
echo Cipher text- jy
:true135
echo Cipher text- as
:true136
echo Cipher text- kv
:true137
echo Cipher text- 5g
:true138
echo Cipher text- j8
:true139
echo Cipher text- s2
:true140
echo Cipher text- sk
:true141
echo Cipher text- 34
:true142
echo Cipher text- gx
:true143
echo Cipher text- 7h
:true144
echo Cipher text- sr
:true145
echo Cipher text- mb
:true146
echo Cipher text- 6g
:true147
echo Cipher text- 8i
:true148
echo Cipher text- id
:true149
echo Cipher text- 28
:true150
echo Cipher text- s6
:true151
echo Cipher text- 4k
:true152
echo Cipher text- 6h
:true153
echo Cipher text- 3f
:true154
echo Cipher text- jf
:true155
echo Cipher text- 8d
:true156
echo Cipher text- 2s
pause > nul
exit
答案 0 :(得分:2)
批处理文件不会停止在标签上处理,它们只是移动到下一行并继续处理,因此程序将处理匹配%num1%的第一个字符上的goto
,跳转到该标签并运行到程序的末尾。
因此,在每个标签中,您需要添加goto :eof
,它将跳转到文件的末尾。如果你这样做,你将只处理%num1%的匹配,因为它将跳转到匹配的标签,然后跳到文件的末尾,退出程序。
您可以使用goto
替换if语句中的call
来解决此问题。这将在标签处运行代码,当代码跳转到文件末尾时,它将在调用后返回到行。
最后,您需要在if块的末尾添加goto :eof
,以防止批处理文件在每个标签语句中运行。
类似于(显示一小段代码)
if %num6%==y call :true155
if %num6%==z call :true156
pause > nul
:true1
echo Cipher text- pl
goto :eof
:true2
echo Cipher text- vd
goto :eof
答案 1 :(得分:2)
虽然其他答案说明了你的程序的直接问题,但我无法抗拒对它进行一些观察的诱惑。您应该研究批处理文件功能并尝试使用它们来编写更好(更简单,更快)的程序。在所有编程语言中都是如此,特别是在批处理文件中;否则你会编写基本和粗略的程序,这些程序当然不是批处理文件的好例子。例如:
1-您应该意识到,如果您检查六个输入字符中的每一个并执行具有相同转换的的六个不同部分,那么这将浪费代码!您可以只编写一次转换集,然后在六个不同的部分中使用它。这个概念被称为“子程序”。
2-一般来说,如果你多次编写相同的代码段而只是改变一小部分,那么你就不会使用编程语言的功能来实现这一点。您可以编写代码的“常规格式”,并在必须更改的部分中放置“参数”,然后让编程语言在一系列值上自动更改该参数。这是一个“循环”,在批处理文件中实现它的方法是通过FOR
命令。
3-编程语言中最重要的概念之一是数组,它允许您在一个元素上指定某个操作,然后以相同的方式在一系列元素上重复该操作只需更改索引即可。如果在FOR
循环中组合一个数组,则可以在几行代码中实现大而复杂的计算。
是的,我知道这些方法通常是通过经验获得的;但是,我只是想把它们呈现给你,这样你才能意识到它们存在。下面有一个批处理文件,它使用这些方法来实现原始程序的相同结果,但是要小得多。
@echo off
setlocal EnableDelayedExpansion
for %%a in ("a=pl" "b=vd" "c=ed" "d=jy" "e=as" "f=kv" "g=5g" "h=j8" "i=s2" "j=sk" "k=34" "l=gx" "m=7h"
"n=sr" "o=mb" "p=6g" "q=8i" "r=id" "s=28" "t=s6" "u=4k" "v=6h" "w=3f" "x=jf" "y=8d" "z=2s") do (
set letter_%%~a
)
rem Read the characters
for /L %%i in (1,1,6) do (
set /p num%%i=Character%%i:
)
rem Check the characters
for /L %%i in (1,1,6) do for %%c in (!num%%i!) do (
if defined letter_%%c echo Cipher text- !letter_%%c!
)
pause > nul
exit
答案 2 :(得分:1)
没有返回goto
语句可以返回并继续处理,因此只会处理第一个goto
,然后通过以下所有内容进行处理。
以下示例将执行您正在尝试的内容,但代码和错误检查更少:
@echo off
setlocal EnableDelayedExpansion
set "Dictionary=00,pl,vd,ed,jy,as,kv,5g,j8,s2,sk,34,gx,7h,sr,mb,6g,8i,id,28,s6,4k,6h,3f,jf,8d,2s"
set "Count=1"
:Prompt
set "Input="
set /p "Input=Character%Count%:"
if not defined Input goto Prompt
set "Input=%Input:"=%"
for /f "delims=abcdefghijklmnopqrstuvwxyz" %%A in ("%Input%") do goto :Error
echo %Input%|choice /c abcdefghijklmnopqrstuvwxyz /n >nul
set /a "Index=%ErrorLevel% * 3"
echo Cipher text- !Dictionary:~%Index%,2!
set /a "Count+=1"
if %Count% gtr 6 goto Stop
goto Prompt
:Error
echo Invalid Input
goto Prompt
:Stop
pause >nul
endlocal
C:\Users\User\Desktop>script.bat
Character1:a
Cipher text- pl
Character2:b
Cipher text- vd
Character3:c
Cipher text- ed
Character4:d
Cipher text- jy
Character5:z
Cipher text- 2s
Character6:a
Cipher text- pl