嘿!我正在尝试做一些数据输入验证,但我一直无法弄清楚。当我尝试验证输入的第一个字符是否是字母时,我得到一个无限循环。 。 。 。无限循环在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(error == true)可以写为while(error) – basszero 2010-02-26 15:43:20
这是一项家庭作业吗? – 2010-02-26 16:16:31