Java 7 Switch Case PRO

As of Java 7 we can write a switch case statement that works on strings. This new capability might assist us with writing a shorter code.

package com.abelski.samples;

public class Demo
{
	public static void main(String[] args)
	{
		String operator = args[1];
		double numA = Double.parseDouble(args[0]);
		double numB = Double.parseDouble(args[2]);
		String result;
		switch(operator)
		{
		case "+":
			result = numA+"+"+numB+"="+(numA + numB);
			break;
		case "-":
			result = numA+"-"+numB+"="+(numA-numB);
			break;
		case "*":
			result = numA+"*"+numB+"="+(numA*numB);
			break;
		case "/":
			result = numA+"/"+numB+"="+(numA/numB);
			break;
		default:
			result = "you can use one of the following operators +,-,/ or *";
			break;
		}
		System.out.println(result);
	}
}

The following video clip overviews this code sample and shows its execution.

Share:

banner for the css playlist in hebrew life michael courses for programmers

The First Steps in CSS

Learn CSS using our our videos (in Hebrew) on the CSS (he) playlist on youtube. Do it now. Do it for free.

Good Trainers Collaborate with Others

It is always essential to keep an open mind and learn from others. This applies to everyone, including teachers and especially software development trainers. Software

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Update cookies preferences