java - Why does this not work? I am trying to write a small program that prompts a user to enter any double value -
java - Why does this not work? I am trying to write a small program that prompts a user to enter any double value -
so have prompt user come in double value if not double value supposed flush buffer prompt user value, print value if double.
double value; scanner sc = new scanner(system.in); system.out.println("enter double value: "); value = sc.nextdouble(); while (!sc.hasnextdouble()) { sc.nextline(); system.out.println("enter valid value: "); value = sc.nextdouble(); } system.out.println(value);
swap order of loop,
// value = sc.nextdouble(); while (!sc.hasnextdouble()) // <-- while don't have double { sc.nextline(); system.out.println("enter valid value: "); // value = sc.nextdouble(); } value = sc.nextdouble(); // <-- here java osx netbeans
Comments
Post a Comment