Java

Switch Statement in Java

public class Switch1
{
    public static void main(String[] args) {
        int x=3;
        switch(x)
        {
            case 0:
            System.out.println("0"); 
            break;
            case 1:
            System.out.println("1");
            break;
            case 2:
            System.out.println("2");
            break;
            case 3:
            System.out.println("3");
             break;
            default:
            System.out.println("Differt Number");
        }
    } 
}

Output

3

public class Switch2
{
    public static void main(String[] args) {
        int x=1;
        switch(x)
        {
            case 0:
            System.out.println("John"); 
            break;
            case 1:
            System.out.println("Anne");
            break;
            case 2:
            System.out.println("Peter");
            break;
            
            default:
            System.out.println("Differt");
        }
    } 
}

Output

Anne

public class Switch1
{
    public static void main(String[] args) {
      
        switch(0)
        {
            case 0:
            System.out.println("John"); 
            break;
            case 1:
            System.out.println("Anne");
            break;
            case 2:
            System.out.println("Peter");
            break;
            
            default:
            System.out.println("Different ");
        }
    } 
}

Output

John

public class Switch2
{
    public static void main(String[] args) {
      
        switch(1)
        {
            case 0:
            System.out.println("John"); 
            
            case 1:
            System.out.println("Anne");
            
            case 2:
            System.out.println("Peter");
           
            
            default:
            System.out.println("Differt ");
        }
    } 
}

Output

Anne
Peter
Differt

Example of Calculator

import java.util.*;
class Example{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("Enter Number 1 : ");
int number1 = input.nextInt();
System.out.print("Enter Number 2 : ");
int number2 = input.nextInt();
System.out.println();
System.out.println("A. Adition");
System.out.println("B. Subtraction");
System.out.println("C. Division");
System.out.println("D. Multiplication.");
System.out.println();
System.out.print("Please select one option : ");
String option = input.next();
System.out.println();
switch(option){
case "A":
System.out.println("Add : " + (number1 + number2));
break;
case "B":
if(number1 > number2){
System.out.println("Sub : " + (number1 - number2));
}else{
System.out.println("Sub : " + (number2 - number1));
}
break;
case "C":
if(number1 > number2){
System.out.println("Sub : " + (number1/number2));
}else{
System.out.println("Sub : " + (number2/number1));
}
break;
case "D":
System.out.println("Multi : " + (number1 * number2));
break;
default :
System.out.println("Please select valid option");
}
}
}

Output

Enter Number 1 : 45
Enter Number 2 : 67

A. Adition
B. Subtraction
C. Division
D. Multiplication.

Please select one option : A

Add : 112

Grade Calculation

import java.util.*;
class Example{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("Enter Student Name : ");
String name = input.next();
System.out.print("Enter Student Address : ");
String address = input.next();
System.out.print("Enter Number of Subjects : ");
int number_of_subjects = input.nextInt();
System.out.println();
int marks = 0;
int max = 0;
int total = 0;
for(int i = 0 ; i < number_of_subjects ; i++){
System.out.print("Enter Subject " + (i + 1) + " Marks : ");
marks = input.nextInt();
if(max < marks){
max = marks;
}
total = total + marks;
}
boolean flag = true;
while(flag){
System.out.println();
System.out.println("A. Show Student Details.");
System.out.println("B. Show Maximum Marks.");
System.out.println("C. Show Total, Average & Result.");
System.out.println("X. Exit.");
System.out.println();
System.out.print("Please select one option : ");
String option = input.next();
System.out.println();
switch(option){
case "A":
System.out.println("Student Name : " + name);
System.out.println("Student Address : " + address);
break;
case "B":
System.out.println("Maximum Marks : " + max);
break;
case "C":
double average = total / (double)number_of_subjects;
String result = "Fail";
if(average >= 50){
result = "Pass";
}
System.out.println("Total : " + total);
System.out.println("Average : " + average);
System.out.println("Result : " + result);
break;
case "X":
System.out.println("Good bye....");
flag = false;
break;
default :
System.out.println("Please select valid option.");
}
}
}
}

Output

Enter Student Name : Raja
Enter Student Address : India
Enter Number of Subjects : 5

Enter Subject 1 Marks : 60
Enter Subject 2 Marks : 80
Enter Subject 3 Marks : 60
Enter Subject 4 Marks : 45
Enter Subject 5 Marks : 60

A. Show Student Details.
B. Show Maximum Marks.
C. Show Total, Average & Result.
X. Exit.

Please select one option : A

Student Name : Raja
Student Address : India

A. Show Student Details.
B. Show Maximum Marks.
C. Show Total, Average & Result.
X. Exit.

Please select one option :

Break

class Example{
public static void main(String args[]){
for(int i = 1 ; i <= 10 ; i++){
if(i == 5){
break;
}
System.out.println(i);
}
System.out.println("Hello Java");
}
}

Output

1
2
3
4
Hello Java

Return

class Example{
public static void main(String args[]){
for(int i = 1 ; i <= 10 ; i++){
if(i == 5){
return;
}
System.out.println(i);
}
System.out.println("Hello Java");
}
}

Output

1
2
3
4

Continue

class Example{
public static void main(String args[]){
for(int i = 1 ; i <= 10 ; i++){
if(i == 5){
continue;
}
System.out.println(i);
}
System.out.println("Hello Java");
}
}

Output

1
2
3
4
6
7
8
9
10
Hello Java

import java.util.*;
class Example{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int marks = 0;
int total = 0;
for(int i = 0 ; i < 5 ;){
System.out.print("Enter Subject " + (i + 1) + " Marks : ");
marks = input.nextInt();
System.out.println();
if(marks > 100){
System.out.println("Invali Marks...");
System.out.println("Please Re-Enter...");
System.out.println();
continue;
}else if(marks < 0){
System.out.println("Invali Marks...");
System.out.println("Please Re-Enter...");
System.out.println();
continue;
}
total = total + marks;
i++;
}
System.out.println("Total : " + total);
}
}

Output

Enter Subject 1 Marks : 67

Enter Subject 2 Marks : 89

Enter Subject 3 Marks : 54

Enter Subject 4 Marks : 67

Enter Subject 5 Marks : 89

Total : 366

 

class Example{
public static void main(String args[]){
String word = "Apple";
String word2 = "Apple";
if(word == word2){
System.out.println("Same");
}else{
System.out.println("Not Same");
}
}
}

Output

Same

 

import java.util.*;
class Example{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int[] number = new int[5];
for(int i = 0 ; i < 5 ; i++){
System.out.print("Enter Number " + (i + 1) + " : ");
number[i] = input.nextInt();
}
System.out.println();
System.out.print("All Numbers : [ ");
for(int i = 0 ; i < number.length ; i++){
System.out.print(number[i] + " ");
}
System.out.println("]");
}
}

Output

Enter Number 1 : 56
Enter Number 2 : 78
Enter Number 3 : 45
Enter Number 4 : 67
Enter Number 5 : 89

All Numbers : [ 56 78 45 67 89 ]

import java.util.*;
class Example{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
while(true){
System.out.print("Do you want to add new Student.? (Y/N) : ");
String letter = input.next();
System.out.println();
switch(letter){
case "Y":
System.out.print("Enter Student Name : ");
String name = input.next();
System.out.print("Enter Student Address : ");
String address = input.next();
System.out.print("Enter Number of Subjects : ");
int number_of_subjects = input.nextInt();
System.out.println();
int[] marks = new int[number_of_subjects];
int max = 0;
int total = 0;
for(int i = 0 ; i < number_of_subjects ;){
System.out.print("Enter Subject " + (i + 1) + " Marks : ");
marks[i] = input.nextInt();
if(marks[i] > 100){
System.out.println("Marks is Grater than 100.");
System.out.println();
continue;
}
if(max < marks[i]){
max = marks[i];
}
total = total + marks[i];
i++;
}
boolean flag = true;
while(flag){
System.out.println();
System.out.println("A. Show Student Details.");
System.out.println("B. Show All Marks.");
System.out.println("C. Show Maximum Marks.");
System.out.println("D. Show Total,Average & Result.");
System.out.println("X. Exit.");
System.out.println();
System.out.print("Please select one option : ");
letter = input.next();
System.out.println();
switch(letter){
case "A":
System.out.println("Student Name : " + name);
System.out.println("Student Address : " + address);
break;
case "B":
for(int i = 0 ; i < marks.length ; i++){
System.out.println("Subject " + (i + 1) + " Marks : " + marks[i]);
}
break;
case "C":
System.out.println("Maximum marks : " + max);
break;
case "D":
double average = (double)total / number_of_subjects;
String result = "pass";
if(average < 50){
result = "Fail";
}
System.out.println("Total : " + total);
System.out.println("Average : " + average);
System.out.println("Result : " + result);
break;
case "X":
System.out.println("Have a nice day..!");
flag = false;
break;
default :
System.out.println("Please select valid option..");
}
}
break;
case "N":
System.out.println("Good bye....");
return;
default :
System.out.println("Please enter valid letter..");
}
System.out.println();
}
}
}

Output

Do you want to add new Student.? (Y/N) : Y

Enter Student Name : John
Enter Student Address : India
Enter Number of Subjects : 3

Enter Subject 1 Marks : 78
Enter Subject 2 Marks : 89
Enter Subject 3 Marks : 54

A. Show Student Details.
B. Show All Marks.
C. Show Maximum Marks.
D. Show Total,Average & Result.
X. Exit.

Please select one option : A

Student Name : John
Student Address : India

A. Show Student Details.
B. Show All Marks.
C. Show Maximum Marks.
D. Show Total,Average & Result.
X. Exit.

Please select one option : A

 

 

 

admin

Recent Posts

Creating Beautiful Login Form Design Using React

Introduction to Login Form Design Designing an effective and beautiful login form is crucial for…

3 days ago

Creating Responsive Login Form with React

Introduction In today creating a responsive login form is essential for providing a seamless user…

5 days ago

Master React Inventory Management System Development

Introduction to Inventory Management Systems In today's fast-paced digital environment, businesses require efficient inventory management…

1 week ago

React Inventory Management System

Introduction to React Inventory Management Systems In today’s fast-paced business environment, efficient inventory management is…

1 week ago

Login Form Using React

How to make a Login Form in React Step by Step. (more…)

2 weeks ago

Building Functional Calculator in React for Beginners

Introduction to React Calculator Creating a functional calculator in React is an excellent way to…

2 weeks ago