in java programming use a scanner class along with switch case statement using numbers -
can tell me how use "switch-case" numbers along scanner class, number read keyboard should compare switch cases given , final output should printed?
import java.util.scanner; public class switchcase{ public static void main(string[] args) { int age; scanner bhavya = new scanner(system.in); system.out.println("enter age:"); age = bhavya.nextint(); switch (age) { case 1: system.out.println("you can crawl"); break; case 2: system.out.println("you can talk"); break; case 3: system.out.println("you can in trouble"); break; default: system.out.println("i dnt know how old are"); break; } }
your program works ... apart fact not dealing case user enters not valid integer.
you need either use scanner.hasnextint()
test if next token integer or catch , diagnose exception thrown scanner.nextint()
when can't read int
.
Comments
Post a Comment