嘿!我正在嘗試做一些數據輸入驗證,但我一直無法弄清楚。當我嘗試驗證輸入的第一個字符是否是字母時,我得到一個無限循環。 。 。 。無限循環在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