无限循环在Java中
嘿!我正在尝试做一些数据输入验证,但我一直无法弄清楚。当我尝试验证输入的第一个字符是否是字母时,我得到一个无限循环。 。 。 。无限循环在Java中
感谢您的帮助!
public class methods
{
public static void main(String args[]) throws IOException
{
String input ="";
int qoh=0;
boolean error=true;
Scanner keyboard = new Scanner (System.in);
//while (error)
//{
//error=true;
while (error==true)
{
System.out.print("\nEnter Quantity on Hand: ");
input = keyboard.nextLine();
if (input.length() <1)
{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
error=true;
System.out.println(qoh);
System.out.println(input);
}
else
{
error=false;
}
}
error = true;
while (error==true)
{
if (Character.isLetter(input.charAt(0)))
{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
error=true;
System.out.println(qoh);
System.out.println(input);
}
else
{
qoh = Integer.parseInt(input);
error=false;
}
}
}
}
您的第二个while循环中没有input = keyboard.nextLine();
。
您可以重构您的代码,以便在发生错误时仅询问新输入。所以,在'错误...'的系统出现之后,我会实际做到这个不同。开头的'error = true'有点令人困惑,因为可能没有错误。
例如,您可以编写一个名为tryProcessLine方法,它读取输入,如果有错误,则返回true,如果确定与假,并不仅仅是做类似下面while(!tryProcessLine()){ }
工作例如:
import java.io.IOException;
import java.util.Scanner;
public class Methods {
private static int qoh;
public static void main(String args[]) throws IOException {
while (!tryProcessLine()) {
System.out.println("error... Trying again");
}
System.out.println("succeeded! Result: " + qoh);
}
public static boolean tryProcessLine() {
String input = "";
Scanner keyboard = new Scanner(System.in);
System.out.print("\nEnter Quantity on Hand: ");
input = keyboard.nextLine();
try {
qoh = Integer.valueOf(input);
if (qoh < 0 || qoh > 500) {
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
return false;
} else {
return true;
}
} catch (NumberFormatException e) {
System.out.println("\n**ERROR06** - Quantity on hand must be numeric");
return false;
}
}
}
-1:这不是无限循环的原因。第一个循环被设计为读取第一个非空行,而我认为第二个循环应该检查这行只包含数字字符(而不是读取另一行输入)。 – Adamski 2010-02-26 15:31:55
我想他想在第二个循环发生错误时得到一个新的输入。否则第二个循环不应该是一个循环。因为现在当第二个循环出现'错误'时,不需要新的输入,并且'error == true'将始终为真... – Fortega 2010-02-26 15:33:58
我的猜测是他想要读取第一个非空行输入并尝试将其解析为整数。我不认为OP需要两个循环。 – Adamski 2010-02-26 15:45:34
发生死循环是因为第二个while循环是反复检查字符串中的第一个字符(input.charAt(0)
)是否是字母。假设这个检查的结果是真的,循环将永远不会终止。
您的代码可以简化为这样的:
Integer qty = null;
while (scanner.hasNext() && qty == null) {
String line = scanner.next();
try {
qty = Integer.parseInt(line);
} catch(NumberFormatException ex) {
System.err.println("Warning: Ignored non-integer value: " + line);
}
}
if (qty == null) {
System.err.println("Warning: No quantity specified.");
}
如果它是一个字符,你允许误差仍然=真,这是造成该循环永远继续下去,你永远不回到开始并阅读另一行。
下面是一些代码,它可以实现你想要的,并且结构更好一些。
public class ScanInfo {
Scanner keyboard = new Scanner(System.in);
public ScanInfo(){
String line = getLineFromConsole();
while(null != line && !"quit".equals(line)){
if(isValidInput(line)){
int validNumber = Integer.parseInt(line);
System.out.println("I recieved valid input: "+validNumber);
}else{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
}
line = getLineFromConsole();
}
}
private boolean isValidInput(String line){
//basic sanity
if(null == line || line.length() < 1){
return false;
}
try {
int number = Integer.parseInt(line);
return (number >= 0 && number <= 500);
} catch (NumberFormatException e) {
return false;
}
}
public static void main(String[] args) {
new ScanInfo();
}
public String getLineFromConsole(){
System.out.print("\nEnter Quantity on Hand: ");
return keyboard.nextLine();
}
}
问题是,在本节:
while (error==true)
{
if (Character.isLetter(input.charAt(0)))
{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
error=true;
System.out.println(qoh);
System.out.println(input);
}
else
{
qoh = Integer.parseInt(input);
error=false;
}
}
一旦你在第一位置的字母,这个循环可以永远终止。它检查一封信是否在第一个位置(它是),打印它并重复。尝试更改为:
while (error==true)
{
if (Character.isLetter(input.charAt(0)))
{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
error=false;
...
此外,一对夫妇的其他东西:
while (error == true)
可缩短至while(error)
。
另外,Integer.parseInt
将抛出NumberFormatException
如果输入不是一个整数 - 你需要捕捉并处理这个。
另外,为什么你需要第二个循环呢?它似乎只是为了验证输入 - 如果是的话,你可以将这个逻辑移入第一个循环,并消除第二个循环。只对需要重复发生的事情使用循环(如用户输入输入数据)。不需要重复检查相同的输入。
备注:while(error == true)可以写为while(error) – basszero 2010-02-26 15:43:20
这是一项家庭作业吗? – 2010-02-26 16:16:31