我有这个程序,它取一个单词的数组,并要求用户输入一个句子,其中包含数组中的每个单词:
@words = qw(Hi world thanks);
foreach $word (@words)
{
print "Enter a line with word:$word\n";
chomp($input = <STDIN>);
if($input=~/$word/i)
{
print "Great\n";
} else {
print "Incorrect. Type a line containing $word\n";
}
}
Run Code Online (Sandbox Code Playgroud)
如果用户使用单词键入输入,则可以正常工作.但如果他不这样做它只打印错误信息并移动到下一个单词.我希望它提示用户重新输入相同单词的输入.但是怎么样?我接下来尝试了它没有用.
cod*_*ict 19
redo在这种情况下,您可以使用a 重新启动当前迭代.
foreach my $word (@words)
{
print "Enter a line with word:$word\n";
chomp($input = <STDIN>);
if($input=~/$word/i)
{
print "Great\n";
} else {
print "Incorrect. Type a line contaning $word\n";
redo; # restart current iteration.
}
}
Run Code Online (Sandbox Code Playgroud)
一个不太推荐的替代方案是使用goto:
foreach my $word (@words)
{
INPUT:print "Enter a line with word:$word\n";
chomp($input = <STDIN>);
if($input=~/$word/i)
{
print "Great\n";
} else {
print "Incorrect. Type a line contaning $word\n";
goto INPUT;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1522 次 |
| 最近记录: |