Question
Asked Nov 16, 2019
14 views

How do I creat a switch statement in java for directions? Ex. North, South, East, West.

check_circle

Expert Answer

Step 1

Switch statement: 

In Java, the switch statement is used to select a single statement out of multiple statements which are being executed.

There can be multiple cases which are compared with the value of the given expression.

Break keyword is used to break the switch statement when a match is found in any of the given cases.

If the user enters input and no match is found, then there is a default case.

 

Syntax:

switch(expression)

{

case 1:

//block of code

//break statement

break;

case 2:

//block of code

//break statement

break;

case 3:

//block of code

//break statement

break;

default:

//block of code

}

Step 2

Below are the program-plan and the sample code to create a switch statement for directions.

Program-plan: 

  • Importing the essential packages and declaring the main class.
  • Inside the main class, the main method is declared.
  • Creating the object for the scanner class.
  • Displaying message and prompt the user to take string input.
  • Using the switch statement and pass the string variable as an argument to compare with the different cases. Also, using .toUpperCase() function to convert lowercase letters to uppercase.
  • In the program, there are four cases to compare for each direction.
  • Now, Comparing the input string with different cases. If the match is found, then the given direction will display, and the break statement will ignore the other cases.
  • If no match is found, then the default case will execute.   

 

Sample Code:

//importing the package

import java.util.*;

//declaring the main class

class Main

{

    //declaring the main function

    public static void main(String s[])

    {

        //creating the object of scanner class

        Scanner obj = new Scanner(System.in);

        //displaying the message

        System.out.println("Please enter the first letter of the direction");

        //prompt user to take String input

        String dir = obj.nextLine();

       

        //using the switching statements

        //.toUpperCase() is used convert lower case input to uppercase

        switch(dir.toUpperCase())

        {

            //case 1 for the north direction

            case "N":

                System.out.println("You are in the North Direction");

                //using the break statement

       ...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Java

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Write a program with python using random module for a (very) rudimentary shooter "game". You are the...

A: Program Algorithm-START-Step1: Import the essential modulesStep2: Define the shoot function.Step3: U...

question_answer

Q: DISCUSS HOW THE CRYPTO CURRENCIES OFFER BENEFITS IN THE DOMAIN OF PERSONAL CRYPTOSECURITY?

A: Security In Cryptocurrency:⦁ Cryptocurrency, if once authenticated then no one can reverse it. As we...

question_answer

Q: Compare and contrast only two types of computer data storage approaches.

A: Comparing and contrasting two data storage approaches:

question_answer

Q: Given a nested list named table, use nested loops to print each element in the nested list.  Each el...

A: Python program to all the elements in a nested listProgram IntroCreate a python program to print all...

question_answer

Q: Write a C++ program: A palindrome is a word that reads the same forwards and backwards for example "...

A: Program:#include<iostream>#include<bits/stdc++.h>using namespace std;void Palindrome_Che...

question_answer

Q: Write a query to display the book number, title, and cost for all books in the “Database” subject so...

A: The name of the table is the subject.Attributes of the table are book_number, book_title, book_costC...

question_answer

Q: Using C++ 11 Create a class called Complex for performing arithmetic with complex numbers. Write a p...

A: This program demonstrates the complex numbers for performing arithmetic.Program://Header file sectio...

question_answer

Q: IN PYTHON Linked Lists Consider the implementation of the Linked list class, implement the following...

A: A Python program for the given scenarios is as follows,import random# Node class class Node:    # Co...

question_answer

Q: *Initial Class Diagram is attached below Refine the UML class diagram so that it demonstrates the fo...

A: The class diagram defines the types of objects within the system and distinct kind of relationship w...