除Q和q外,我的代码无法处理任何数字或字母。您必须输入1到20之间的整数或Q或q才能退出,但是一旦输入了1-20的数字,它就不会执行任何操作,并且当它是Q或q以外的数字时,不会显示无效的输入消息。请快速帮助。
String x;
long y=1;
int newint=0;
int stop=0;
Scanner newscan=new Scanner (System.in);
System.out.println("Enter an integer between 1 and 20 or Q or q to
quit:") ;
x=newscan.nextLine();
while (stop==0)
if (x.compareTo("Q")==0||x.compareTo("q")==0)
return;
if (x.compareTo("0")>0 && x.compareTo(";")<0)
{newint=Integer.parseInt(x);
if(newint>=1 && newint<=20)
{y=1;
while(newint>1)
{y=newint*y;
newint=newint-1;}
System.out.println("The Factorial is "+y);}
System.out.println("Enter an integer between 1 and 20 or Q or q to
quit:") ;
x=newscan.nextLine(); }
else
{ System.out.println("Invalid input");
System.out.println("Enter an integer between 1 and 20 or Q or q to
quit:") ;
x=newscan.nextLine(); }