Detect key press

  Detect key press


Problem Description


Write a Java program to detect key presses.


If the user pressed number keys( from 0 to 9), the program will tell the number that is pressed, otherwise, program will show ""Not allowed"


Solution,


import java.util.Scanner;

import java.io.*;

public class TestClass {

public static void main(String[] args) { 

Scanner sc= new Scanner(System.in); 

     int a= sc.nextInt();

     if(a<=9)

     {

System.out.println("You pressed "+a);  

     }

       else

       {

         System.out.println("Not allowed");

     }

     }

}

Post a Comment

0 Comments