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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -