java - What's wrong with For Loop? -
package mygradeloops; import java.io.ioexception; public class mygradeloops { public static void main(string[] args) throws ioexception { char x = 'a'; (x='0';x<'9';x++){ system.out.println("please enter in 1 of grades."); system.in.read(); system.out.println("keep going!"); } } }
this code keeps double printing after first "grade". know why double prints? have done "for loop" wrong?
it's "double printing" because when enter character pressing return, you're writing 2 characters: character typed, , \n
(newline character).
add second system.in.read();
call read newline character:
for (x='0';x<'9';x++){ system.out.println("please enter in 1 of grades."); system.in.read(); // character system.in.read(); // newline system.out.println("keep going!"); }
also, initializing x
'a'
in not needed, char x;
fine. in fact, doesn't make sense use char
in loop, using int
preferred.
Comments
Post a Comment