GVKun编程网logo

在while循环中进行扫描仪输入验证(在while循环中进行扫描仪输入验证的方法)

15

本文将为您提供关于在while循环中进行扫描仪输入验证的详细介绍,我们还将为您解释在while循环中进行扫描仪输入验证的方法的相关知识,同时,我们还将为您提供关于bash–在while循环中cd进入目

本文将为您提供关于在while循环中进行扫描仪输入验证的详细介绍,我们还将为您解释在while循环中进行扫描仪输入验证的方法的相关知识,同时,我们还将为您提供关于bash – 在while循环中cd进入目录不起作用、bash – 如何在while循环中“读取”变量、Bash脚本,在while循环中有多个条件、bash:在while循环中反击(kill和kill -9)的实用信息。

本文目录一览:

在while循环中进行扫描仪输入验证(在while循环中进行扫描仪输入验证的方法)

在while循环中进行扫描仪输入验证(在while循环中进行扫描仪输入验证的方法)

我必须在 while 循环中显示Scanner输入:用户必须插入输入,直到他写下“ quit”为止。因此,我必须验证每个输入以检查他是否写出“
quit”。我怎样才能做到这一点?

while (!scanner.nextLine().equals("quit")) {    System.out.println("Insert question code:");    String question = scanner.nextLine();    System.out.println("Insert answer code:");    String answer = scanner.nextLine();    service.storeResults(question, answer); // This stores given inputs on db}

这行不通。如何验证每个用户的输入?

答案1

小编典典

问题是nextLine()
“使该扫描程序前进到当前行之外”。因此,当您调用nextLine()while条件并且不保存返回值时,您将丢失用户输入的那一行。nextLine()对第3行的调用返回另一行。

您可以尝试这样的事情

    Scanner scanner=new Scanner(System.in);    while (true) {        System.out.println("Insert question code:");        String question = scanner.nextLine();        if(question.equals("quit")){            break;        }        System.out.println("Insert answer code:");        String answer = scanner.nextLine();        if(answer.equals("quit")){            break;        }        service.storeResults(question, answer);    }

bash – 在while循环中cd进入目录不起作用

bash – 在while循环中cd进入目录不起作用

我有以下脚本:

#!/bin/bash

ls -1 | while read d
do 
    [[ -f "$d" ]] && continue
    echo $d
    cd $d
done

问题是每张cd都说“[路径]:没有这样的文件或目录”,为什么?
文件夹存在,因为我列出它…

解决方法

我在你的代码中看到两个问题:

>您不测试目录.
>一旦你进入了dir,就会留在那里.

请试试这个:

#!/bin/bash

ls -1 | while read d
do 
    test -d "$d" || continue
    echo $d
    (cd $d ; echo "In ${PWD}")
done

bash – 如何在while循环中“读取”变量

bash – 如何在while循环中“读取”变量

读取行时如何读取变量?

例如:

the_list=$(..code..)

while read line
do
        echo $line

done < $the_list

使用上面的代码给我错误:

./copy.sh: line 25: $the_list: ambiguous redirect
你可以写:
while IFS= read -r line
do
    echo "$line"
done <<< "$the_list"

见§3.6.7 “Here Strings” in the Bash Reference Manual

(我也有机会添加一些双引号,并添加-r和IFS =来阅读,以避免太多的内容与你的变量的内容混淆。)

Bash脚本,在while循环中有多个条件

Bash脚本,在while循环中有多个条件

我试图得到一个简单的while循环工作在bash使用两个条件,但尝试了许多不同的语法从各种论坛,我不能停止抛出一个错误。这里是我有:
while [ $stats -gt 300 ] -o [ $stats -eq 0 ]

我也试过:

while [[ $stats -gt 300 ] || [ $stats -eq 0 ]]

…以及其他几个结构。我想要这个循环继续,而$ stats是> 300或$ stats = 0。

正确的选项是(按推荐顺序):
# Single POSIX test command with -o operator (not recommended anymore).
# Quotes strongly recommended to guard against empty or undefined variables.
while [ "$stats" -gt 300 -o "$stats" -eq 0 ]

# Two POSIX test commands joined in a list with ||.
# Quotes strongly recommended to guard against empty or undefined variables.
while [ "$stats" -gt 300 ] || [ "$stats" -eq 0 ]

# Two bash conditional expressions joined in a list with ||.
while [[ $stats -gt 300 ]] || [[ $stats -eq 0 ]]

# A single bash conditional expression with the || operator.
while [[ $stats -gt 300 || $stats -eq 0 ]]

# Two bash arithmetic expressions joined in a list with ||.
# $ optional,as a string can only be interpreted as a variable
while (( stats > 300 )) || (( stats == 0 ))

# And finally,a single bash arithmetic expression with the || operator.
# $ optional,as a string can only be interpreted as a variable
while (( stats > 300 || stats == 0 ))

一些注意事项:

>引用[[…]]和((…)中的参数扩展是可选的;如果未设置变量,-gt和-eq将假定值为0。>使用$是可选内部((…)),但使用它可以帮助避免无意的错误。如果没有设置stats,那么((stats> 300))将假定stats == 0,但是(($ stats> 300))将产生语法错误。

bash:在while循环中反击(kill和kill -9)

bash:在while循环中反击(kill和kill -9)

所以我最近学会了 kill is not a synchronous命令,所以我在bash中使用while循环,这很棒:
while kill PID_OF_THE_PROCESS 2>/dev/null; do sleep 1; done

但是,有些情况(非常罕见,但它们仍然会发生),其中进程被卡住,并且它不会对kill信号起作用.在这些情况下,杀死应用程序的唯一方法是使用“kill -9”.

所以我想知道,如果循环已达到第10次迭代,如何在bash中修改上面的while循环以使用-9参数?

正如其他用户所说….你必须在使用这种野蛮方法之前解决阻塞的原因……无论如何……试试这个
#!/bin/bash

i=0

PID_OF_THE_PROCESS="your pid you can set as you like"

# send it just once
kill $PID_OF_THE_PROCESS 2>/dev/null;

while [ $i -lt 10 ];
do
    # still alive?
    [ -d /proc/$PID_OF_THE_PROCESS ] || exit;
    sleep 1;
    i=$((i+1))
done

# 10 iteration loop and still alive? be brutal
kill -9 $PID_OF_THE_PROCESS

关于在while循环中进行扫描仪输入验证在while循环中进行扫描仪输入验证的方法的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于bash – 在while循环中cd进入目录不起作用、bash – 如何在while循环中“读取”变量、Bash脚本,在while循环中有多个条件、bash:在while循环中反击(kill和kill -9)的相关知识,请在本站寻找。

本文标签: