0% found this document useful (0 votes)
6 views5 pages

Switch

Java Switch Statements

Uploaded by

Aj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Switch

Java Switch Statements

Uploaded by

Aj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

//Converts a numerical grade into a letter grade using a switch-case statement.

import java.util.Scanner;

public class GradeConverter {


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

System.out.print("Enter your numerical grade (0-100): ");


int grade = scanner.nextInt();

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

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

scanner.close();
}
}
public class TrafficLights {
// Declare the enum outside the main method
enum TrafficLightColor { RED, YELLOW, GREEN }

public static void main(String[] args) {


TrafficLightColor color = TrafficLightColor.GREEN;
switch (color) {
case RED:
System.out.println("Stop!");
break;
case YELLOW:
System.out.println("Prepare to stop.");
break;
case GREEN:
System.out.println("Go!");
break;
default:
System.out.println("Invalid light color.");
break;
}
}
}
import java.util.Scanner;
//MenuOrder
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;

System.out.println("Welcome to the Cafe!");


System.out.println("Please select an item from the menu:");
System.out.println("1. Coffee");
System.out.println("2. Tea");
System.out.println("3. Sandwich");
System.out.println("4. Salad");
System.out.println("5. Exit");

choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.println("You ordered Coffee.");
break;
case 2:
System.out.println("You ordered Tea.");
break;
case 3:
System.out.println("You ordered a Sandwich.");
break;
case 4:
System.out.println("You ordered a Salad.");
break;
case 5:
System.out.println("Exiting the menu. Thank you!");
break;
default:
System.out.println("Invalid choice. Please select a valid menu item.");
break;
}

scanner.close();
}
}

You might also like