While loop issue in Java -
this little bit of code.
system.out.print("would continue (y/n)?"); while (!anwser.equals("y")){ anwser = userinput.next(); system.out.println("would continue (y/n)?"); }
this answer.
> continue (y/n)? > continue (y/n)?
why print out again although typed y , conditions not met? after continues code:
a while
loop done 0 or more times.
a do while
loop done 1 or more times.
for continuation question not care user inputs long 'y' or 'y'. else terminate program. continuation question program wants run once. wrap code in do while
loop.
do { // code goes here system.out.print("would continue (y/n)?"); anwser = userinput.next(); } while (answer.equalsignorecase( "y"));
as why code not work. perhaps should have assigned value answer before loop starts.
Comments
Post a Comment