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

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -