Exercise 1: Write A Java Program To Detect Key Presses.: Solution
Exercise 1: Write A Java Program To Detect Key Presses.: Solution
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.io.*;
}
Exercise 2: Write a Java program that allows the user to choose the
correct answer of a question.
See the example below:
What is the correct way to declare a variable to store an integer value in Java?
a. int 1x=10;
b. int x=10;
c. float x=10.0f;
d. string x="10";
Enter your choice: c
Solution:
import java.io.*;
}catch(IOException e){};
switch (ans)
{
case 'a': System.out.println("Invalid choice!"); break;
case 'b': System.out.println("Congratulation!"); break;
case 'c': System.out.println("Invalid choice!"); break;
case 'd': System.out.println("Invalid choice!"); break;
default: System.out.println("Bad choice!");break;