Site icon JavaGoal

switch statement programs for practices

Here we will discuss the different types of switch statement programs for practices. You can simply try these programs on your end also. switch statement programs for practices:

switch statement programs for practices

1. Write a Java program that takes input from the user and uses a switch statement to perform different actions based on the user’s input.

import java.util.Scanner;

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

        System.out.println("Please enter a number between 1 and 4:");
        int number = input.nextInt();

        switch (number) {
            case 1:
                System.out.println("You entered 1.");
                break;
            case 2:
                System.out.println("You entered 2.");
                break;
            case 3:
                System.out.println("You entered 3.");
                break;
            case 4:
                System.out.println("You entered 4.");
                break;
            default:
                System.out.println("Invalid input. Please enter a number between 1 and 4.");
                break;
        }
    }
}

2. Write a java program that uses a switch statement to print the name of the month corresponding to a given integer value.

import java.util.Scanner;

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

        System.out.println("Please enter a month number (1-12):");
        int month = input.nextInt();

        String monthName;

        switch (month) {
            case 1:
                monthName = "January";
                break;
            case 2:
                monthName = "February";
                break;
            case 3:
                monthName = "March";
                break;
            case 4:
                monthName = "April";
                break;
            case 5:
                monthName = "May";
                break;
            case 6:
                monthName = "June";
                break;
            case 7:
                monthName = "July";
                break;
            case 8:
                monthName = "August";
                break;
            case 9:
                monthName = "September";
                break;
            case 10:
                monthName = "October";
                break;
            case 11:
                monthName = "November";
                break;
            case 12:
                monthName = "December";
                break;
            default:
                monthName = "Invalid month";
                break;
        }

        System.out.println("You entered " + monthName);
    }
}

3. Write a java program that takes a grade letter from the user (A, B, C, D, or F) and prints the corresponding GPA value

import java.util.Scanner;

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

        System.out.println("Please enter a grade letter (A, B, C, D, or F):");
        String grade = input.next().toUpperCase();

        double gpa;

        switch (grade) {
            case "A":
                gpa = 4.0;
                break;
            case "B":
                gpa = 3.0;
                break;
            case "C":
                gpa = 2.0;
                break;
            case "D":
                gpa = 1.0;
                break;
            case "F":
                gpa = 0.0;
                break;
            default:
                gpa = -1.0;
                break;
        }

        if (gpa == -1.0) {
            System.out.println("Invalid grade letter");
        } else {
            System.out.println("Your GPA is " + gpa);
        }
    }
}

4. Write a java program that takes a day of the week from the user (1-7) and prints a corresponding message

import java.util.Scanner;

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

        System.out.println("Please enter a day of the week (1-7):");
        int day = input.nextInt();

        String message;

        switch (day) {
            case 1:
                message = "It's Monday!";
                break;
            case 2:
                message = "It's Tuesday!";
                break;
            case 3:
                message = "It's Wednesday!";
                break;
            case 4:
                message = "It's Thursday!";
                break;
            case 5:
                message = "It's Friday!";
                break;
            case 6:
                message = "It's Saturday!";
                break;
            case 7:
                message = "It's Sunday!";
                break;
            default:
                message = "Invalid day of the week";
                break;
        }

        System.out.println(message);
    }
}

5. Write a java program that converts a numeric grade into a letter grade based on a standard grading scale

import java.util.Scanner;

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

        System.out.println("Please enter a numeric grade (0-100):");
        int grade = input.nextInt();

        String letterGrade;

        switch (grade / 10) {
            case 10:
            case 9:
                letterGrade = "A";
                break;
            case 8:
                letterGrade = "B";
                break;
            case 7:
                letterGrade = "C";
                break;
            case 6:
                letterGrade = "D";
                break;
            default:
                letterGrade = "F";
                break;
        }

        System.out.println("Your letter grade is " + letterGrade);
    }
}

6. Write a java program that calculates the total cost of an order based on the number of items and a per-item cost that varies depending on the type of item

import java.util.Scanner;

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

        System.out.println("Please enter the quantity of items in your order:");
        int quantity = input.nextInt();

        System.out.println("Please enter the type of item in your order (1-3):");
        int itemType = input.nextInt();

        double itemCost;

        switch (itemType) {
            case 1:
                itemCost = 5.0;
                break;
            case 2:
                itemCost = 10.0;
                break;
            case 3:
                itemCost = 15.0;
                break;
            default:
                System.out.println("Invalid item type. Using default cost of $10.0");
                itemCost = 10.0;
                break;
        }

        double totalCost = quantity * itemCost;

        System.out.println("Your total cost is $" + totalCost);
    }
}

7. Write a java program that determines the number of days in a given month and year

import java.util.Scanner;

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

        System.out.println("Please enter a month (1-12):");
        int month = input.nextInt();

        System.out.println("Please enter a year:");
        int year = input.nextInt();

        int numDays;

        switch (month) {
            case 2:
                if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                    numDays = 29;
                } else {
                    numDays = 28;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30;
                break;
            default:
                numDays = 31;
                break;
        }

        System.out.println("There are " + numDays + " days in that month.");
    }
}
Exit mobile version