DEVTOME.COM HOSTING COSTS HAVE BEGUN TO EXCEED 115$ MONTHLY. THE ADMINISTRATION IS NO LONGER ABLE TO HANDLE THE COST WITHOUT ASSISTANCE DUE TO THE RISING COST. THIS HAS BEEN OCCURRING FOR ALMOST A YEAR, BUT WE HAVE BEEN HANDLING IT FROM OUR OWN POCKETS. HOWEVER, WITH LITERALLY NO DONATIONS FOR THE PAST 2+ YEARS IT HAS DEPLETED THE BUDGET IN SHORT ORDER WITH THE INCREASE IN ACTIVITY ON THE SITE IN THE PAST 6 MONTHS. OUR CPU USAGE HAS BECOME TOO HIGH TO REMAIN ON A REASONABLE COSTING PLAN THAT WE COULD MAINTAIN. IF YOU WOULD LIKE TO SUPPORT THE DEVTOME PROJECT AND KEEP THE SITE UP/ALIVE PLEASE DONATE (EVEN IF ITS A SATOSHI) TO OUR DEVCOIN 1M4PCuMXvpWX6LHPkBEf3LJ2z1boZv4EQa OR OUR BTC WALLET 16eqEcqfw4zHUh2znvMcmRzGVwCn7CJLxR TO ALLOW US TO AFFORD THE HOSTING.

THE DEVCOIN AND DEVTOME PROJECTS ARE BOTH VERY IMPORTANT TO THE COMMUNITY. PLEASE CONTRIBUTE TO ITS FURTHER SUCCESS FOR ANOTHER 5 OR MORE YEARS!

Java Selection Statements 3: Switch Statement

Java Selection Statements parts one and two show how to create simple If (single selection statement) and If Else (double selection statement) statements using input from the keyboard. In part three, I will be showing you how to create a pretty simple switch statement. It will be a calculator in the simplest form; add, subtract, multiply and divide.

One thing that we should already know from the If and If Else tutorials is that if you expect to get input from the user via the keyboard, you must import the Scanner.

import java.util.Scanner;

You will always have to add the class when starting the program. Most compilers, such as Net Beans, will automatically add it in for you.

public class SelectSwitch {

Next is the method statement. I will not get into what each part of the method statement means, so for now just know that it allows the program to read and process the strings (characters or words) and arguments (statements, commands or comparisons) within the following curly braces. You need to place an opening curly brace following the statement.

public static void main(String[] args){

The next thing to do is declare your variables. It really doesn’t matter what order you place them in as long as they are declared. In this example the input from the user will be integers (whole numbers, not decimals) and will be declared as x and y. Also, r is will be declared as an integer; r is the selection input for the switch statement; I’ll explain when we get to that part.

int x,y,r;

I will declare z as a double because that is result variable. For example, to divide seven by two would result in an answer with a decimal; 3.5. Declaring as a double will allow decimals.

double z;

Now we have to call the scanner something. Parts one and two I declared the scanner as scan. This time I will call it scnr just to show that the name you choose really doesn’t matter.

Scanner scnr = new Scanner(System.in);

We need to let the user know what to do, so we will print instructions to the screen.

System.out.print(“Enter the first number: ”);

The first number the user enters will be one of the variables we declared earlier. We will use x just because it comes first in the alphabet. So, here we will assign x to the first input from the user.

x=scan.nextInt();

Now we need another number from the user for the calculation.

System.out.print(“Enter the second number: ”);

The variable y will be assigned to the next integer input from the user.

y=scan.nextInt();

The next five lines could be combined into one line and formatted to show on the screen as separate lines. I will place them all on separate lines to make it easier to read and understand. This is to simply list the choices and instructions for the user.

System.out.println(“1 : Addition”);

System.out.println(“2 : Subtraction”);

System.out.println(“3 : Multiplication”);

System.out.println(“4 : Division”);

System.out.print(“Enter the number of the calculation you would like to perform ”);

The user has the option to add, subtract, multiply or divide the two numbers entered. So, we need to assign the variable r (as we discussed earlier, r is the variable for the user’s selection) to the scanner.

r=scnr.nextInt();

The actual switch statement is next. This will perform the calculations based on the selection. Now that r is assigned to the user’s selection we need to pass r on over to the switch statement. Everything in the switch statement needs to be in curly braces, so you will need to add an opening brace here too.

switch( r){

A switch option is called a case and each case name is followed by a colon. Each case’s function is also separated by curly braces. So again, place a curly brace following the case name.

case 1: {

Case 1 is addition. We know that z is the variable for the calculation result and that x and y are the input variables (x being the first number entered and y being the second). So, here we just type the calculation using the variables.

z=x+y;

Then we will want to print the result which is z. You could have it show text as well. For example; (x + “ + ” + y + “ = “ + z) would show on the screen as x + y = z. Of course x, y and z would be real numbers. In my example I only show the result (z).

System.out.println(z);

We are done with case 1, so we need to close the curly brace too.

}

Following each case, there needs to be a “break;”. This tells the program that if the preceding case was selected then stop running the program. If that case is not selected then it continues to the next case.

break;

Now on to case 2. It is exactly the same format.

case 2:{

The second selection was subtraction, so the calculation in case 2 should reflect as such.

z=x-y;

System.out.println(z);

}

Don’t forget the break to stop the program if case 2 is selected.

break;

You could actually take a short cut here and just copy and paste the first cases and change the + and – to * and / for the calculations. If you do this, be sure to change the case numbers to 3 and 4 as well.

case 3:{

z=x*y;

System.out.println(z);

}

break;

case 4:{

z=x/y;

System.out.println(z);

}

break;

When all of the selections or cases are added you need to have a default statement similar to the cases. This is what the program will do if an unexpected selection is made. For example, if the user selects 5 instead of 1, 2, 3, or 4.

default:{

System.out.println(“You must select 1, 2, 3 or 4 for the calculation”);

}

To finish up we need to close the switch, method and class curly braces.

}

}

}

Switch statements may look complicated; however, when you break them down into smaller bits, they really are fairly simple. If you are just starting out in Java programming, I hope I have made selection statements easier to understand.

switch.jpg

You can view my other articles at sharkness

Category: Programming


QR Code
QR Code java_selection_statements_part_3_switch_statement (generated for current page)
 

Advertise with Anonymous Ads