Batch file 批处理文件要求无限次输入

Batch file 批处理文件要求无限次输入,batch-file,Batch File,它应该向用户询问一个号码。它将取这个数字,并在x分钟内关闭计算机,我乘以60将其转换为秒,如果答案是a,它将取消关闭。 当我启动程序时,它会无限次地请求我输入,而根本不调用shutdown 谢谢:试试这个: @echo off :cancel shutdown /a :shut set a=%1 shutdown -s -f -t %a% cls set /p a="the computer will shut in:" %=% set /a a=%a%*

它应该向用户询问一个号码。它将取这个数字,并在x分钟内关闭计算机,我乘以60将其转换为秒,如果答案是a,它将取消关闭。 当我启动程序时,它会无限次地请求我输入,而根本不调用shutdown 谢谢:

试试这个:

@echo off

:cancel

    shutdown /a

:shut

    set a=%1

    shutdown -s -f -t %a%

cls

set /p a="the computer will shut in:" %=%

set /a a=%a%*60

if %a%=="a" call :cancel

pasue

call :shut

pause
代码中的主要问题是执行流。 当没有开关或gotos时,它将执行从第一行到最后一行的命令。 这就是为什么它会无限次地询问您,因为每次您调用:cancel时,它都会再次从:cancel函数执行set/p

你需要把函数放在主代码下面。 在函数中,您需要添加goto:eofeexcept最后一行,因为它是文件的最后一行。 在函数调用之后,还需要添加goto:eof。

尝试以下操作:

@echo off

:cancel

    shutdown /a

:shut

    set a=%1

    shutdown -s -f -t %a%

cls

set /p a="the computer will shut in:" %=%

set /a a=%a%*60

if %a%=="a" call :cancel

pasue

call :shut

pause
代码中的主要问题是执行流。 当没有开关或gotos时,它将执行从第一行到最后一行的命令。 这就是为什么它会无限次地询问您,因为每次您调用:cancel时,它都会再次从:cancel函数执行set/p

你需要把函数放在主代码下面。 在函数中,您需要添加goto:eofeexcept最后一行,因为它是文件的最后一行。
在函数调用之后,您还需要添加goto:eof。

看看您的代码,我想,您把标签和函数声明混淆了。批处理中没有函数,至少不像其他语言,只有call、goto和label

对于显示的任务,您根本不需要任何标签:

::It's better to comment out @echo off, and only when the .bat works fine and then use it.
::@echo off

cls
set /p a="the computer will shut in:"
::You'll need to put %a% inside quotes too, otherwise it can't be equal when you input a.
if "%a%"=="a" call :cancel & goto :eof

:: equals to set /a a=a*60, and with /a you don't need %
set /a a*=60

:: You forgot the parameter. And the goto :eof is necessary
call :shut %a%
pause && goto :eof

::You need to put functions at last.
:cancel
    shutdown /a
    goto :eof
:shut
    set a=%1
    shutdown -s -f -t %a%
注意:代码中存在逻辑故障:set/a始终返回一个整数,只要它不产生语法错误,因此将变量与set/a后面的字符串a进行比较是没有意义的


推荐阅读:

看看你的代码,我想,你把标签和函数声明混淆了。批处理中没有函数,至少不像其他语言,只有call、goto和label

对于显示的任务,您根本不需要任何标签:

::It's better to comment out @echo off, and only when the .bat works fine and then use it.
::@echo off

cls
set /p a="the computer will shut in:"
::You'll need to put %a% inside quotes too, otherwise it can't be equal when you input a.
if "%a%"=="a" call :cancel & goto :eof

:: equals to set /a a=a*60, and with /a you don't need %
set /a a*=60

:: You forgot the parameter. And the goto :eof is necessary
call :shut %a%
pause && goto :eof

::You need to put functions at last.
:cancel
    shutdown /a
    goto :eof
:shut
    set a=%1
    shutdown -s -f -t %a%
注意:代码中存在逻辑故障:set/a始终返回一个整数,只要它不产生语法错误,因此将变量与set/a后面的字符串a进行比较是没有意义的


推荐阅读:

使用Ctrl+K或{}按钮先格式化代码选择。你的意思是我需要执行{shutdown-s-f-t%a%}?不,我的意思是如何在此处格式化帖子,使其看起来正确。使用Ctrl+K或{}按钮先格式化代码选择。你的意思是我需要执行{shutdown-s-f-t%a%}吗?不,我的意思是如何在此处格式化帖子,使其看起来正确。@אבירזז文件结束,这意味着退出当前批处理执行。我发现了问题。我忘了给参数关闭:@אבירזז文件结束,这意味着退出当前正在执行的批处理。我发现了问题。我忘了给parmeter to shut:嗯,函数只是一个概念,因为有参数、调用和返回,所以可以调用函数。嗯,也许更像过程而不是函数:@Tiw:是的,它可以像函数或过程一样使用,我们都经常这样做,甚至有时会调用函数,但从技术上讲,它不是。我个人最喜欢的术语是Subroutine我同意,就像你的术语Subroutine一样:-嗯,函数只是一个概念,因为有参数、调用和返回,所以它可以被称为函数。嗯,也许更像过程而不是函数:@Tiw:是的,它可以像函数或过程一样使用,我们都经常这样做,甚至有时会调用函数,但从技术上讲,它不是。我个人最喜欢的术语是子程序我同意,就像你的术语子程序一样:-