java - Having an incedibly tough time with a loop -
i've copied part of instructions below, , can code pretty every part on own, getting control flow giving me massive doubts ability.
one of biggest problems int gamechanger. im supposed verify if integer or not, , loop if not. im supposed check see if thebuser ever types "exit". input variable scanner instance integer... im stumped. can use try catch check missmatchexception once input being read in, doesnt solve exit issue nor able come solid logic try catch loop if indeed isnt integer. im thinking while loop havent gotten work.
instructions:
you can whether input number before attempting consume it.
int num; while (true) { if (scanner.hasnextint()) { num = scanner.nextint(); break; } else { // read whatever there instead. string line = scanner.nextline(); if (line.equals("exit")) system.exit(0); system.out.println("please enter number"); } } system.out.println("number entered " + num);
Comments
Post a Comment