Java exercises and solutions: switch case


Java switch case

Exercise 1: 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.io.*;


public class JavaExercises
{
public static void main(String[] args)
{
   detectKey();
}

static void detectKey(){

char key=' ';
System.out.print("Press a number key:");
try{
key = (char)System.in.read();
}catch(IOException e){};
switch (key)
{
case '0': System.out.println("You pressed 0."); break;
case '1': System.out.println("You pressed 1."); break;
case '2': System.out.println("You pressed 2."); break;
case '3': System.out.println("You pressed 3."); break;
case '4': System.out.println("You pressed 4."); break;
case '5': System.out.println("You pressed 5."); break;
case '6': System.out.println("You pressed 6."); break;
case '7': System.out.println("You pressed 7."); break;
case '8': System.out.println("You pressed 8."); break;
case '9': System.out.println("You pressed 9."); break;
default: System.out.println("Not allowed!"); break;

      }

  }

}


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.*;

public class JavaExercises
{
public static void main(String[] args)
{
   selectChoice();
}

static void selectChoice(){


char ans=' ';
System.out.println("What is the correct way to declare a variable to store an integer value in Java?");
System.out.println("a. int 1x=10");
System.out.println("b. int x=10");
System.out.println("c. float x=10.0f");
System.out.println("d. string x=\"10\"");
System.out.print("Enter your choice:");
try{
ans = (char)System.in.read();

}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;


}


}

}




Comments

Student comment

 Student

public void correctAnswer() {

String choice;
char ans;

System.out.println("What is the correct way to declare a variable to store an integer value in Java?");
System.out.println("a. int 1x = 10");
System.out.println("b. int x = 10");
System.out.println("c. float x = 10.0f");
System.out.println("d. string x = '10'");
System.out.println();
System.out.println("Enter your choice: ");

Scanner input = new Scanner(System.in);
choice = input.nextLine().toLowerCase();
ans = choice.charAt(0);

if( choice.length() > 1) {
System.out.println("Choices are : a, b, c or d");
} else {
switch(ans) {
case 'a': System.out.println("Incorrect!"); break;
case 'b': System.out.println("Correct!"); break;
case 'c': System.out.println("Incorrect!"); break;
case 'd': System.out.println("Incorrect!"); break;
default: System.out.println("Choices are : a, b, c or d"); break;
}
}
}


2017-11-14
GAURAV  mishra comment

 GAURAV mishra

import java.util.Scanner;
class Question
{
public static void main(String args[])
{
String choice;
char ans;
System.out.println("The legitimate way of declaring is ");
System.out.println(" a - int 1x=10; ");
System.out.println(" b - int x=10; ");
System.out.println(" c - float x=10.0f; ");
System.out.println(" d - string x=\"10\"; ");
System.out.println("Enter your choice : ");
Scanner obj = new Scanner(System.in);
choice=obj.nextLine();
choice=choice.toLowerCase();
ans=choice.charAt(0);
switch(ans)
{
case 'a':
System.out.print(" int 1x=10; is the right choice");
break;

case 'b':
System.out.print(" int x=10; is the right choice");
break;

case 'c':
System.out.print(" float x=10.0f; is the right choice");
break;

case 'd':
System.out.print(" string x=\"10\"; is the right choice");
break;

case 'e':
System.exit(0);
break;

default:
System.out.print("Invalid Answer");

}
}
}


2017-07-25
jeeer comment

 jeeer

thid id


2017-05-29
iTON comment

 iTON

import java.util.Scanner;

public class Exercise8 {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);

char choice;

System.out.println("What is the correct way to declare a variable to store an Integer value in java?");
System.out.println("");
System.out.println("a. int 1x=10");
System.out.println("b. int x=10");
System.out.println("c. float x=10.0f");
System.out.println("d. string x=\"10\"");
System.out.println("");

System.out.print("Enter Your choice: ");
choice =sc.next().charAt(0);

switch (choice) {
case 'a': System.out.println("Invalid Choice!");
break;
case 'b': System.out.println("Congratulations!");
break;
case 'c': System.out.println("Invalid Choice!");
break;
case 'd': System.out.println("Invalid Choice!");
break;
default: System.out.println("Bad Choice!");
}





}

}


2016-05-21
Android Example comment

 Android Example

very nice blog, i have also found one good link here.
<a href="http://androidexample.com/If_Else_Switch_Statements_In_Java/example.php?view=description&aid=147&article=161&mcat=2">If Else Switch Statements In Java</a>


2016-01-08
fanta damtie comment

 fanta damtie

it is very nice! please give an example of grade programming by using his or her id numbers
, name and by using quice,mid exam, final etc


2015-12-12
preeti comment

 preeti

I think it will helpful more than the above code.....................

import java.util.Scanner;
public class switchclass
{
public static void main(String [] args)
{
Scanner sc= new Scanner(System.in);
System.out.println("enter a number");
int a= sc.nextInt();
if(a<9)
switch(a)

{
case 0 :
{
System.out.println("pressed number 0");
break;
}


case 1:
{
System.out.println("pressed number 1");
break;

}

case 2:
{
System.out.println("pressed number 2");
break;

}
case 3:
{
System.out.println("pressed number 3");
break;

}

case 4:
{
System.out.println("pressed number 4");
break;

}
case 5:
{
System.out.println("pressed number 5");
break;

}
case 6:
{
System.out.println("pressed number 6");
break;

}
case 7:
{
System.out.println("pressed number 7");
break;

}
case 8:
{
System.out.println("pressed number 8");
break;

}


// default:

// System.out.println("you are not allowed");
// break;
}
else
{
System.out.println(" not allowed");
}
}
}



2015-06-06



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.