Switch case ...interseting problem
hi i am currently designing a calculator in java and i have a code here that is to handle this code i use if-else statement
if (decision == 1)
{
Multiplication();
}
if (decision == 2)
{
Addition();
}
if (decision == 3)
{
Subtraction();
}
if (decision == 4)
{
Division();
}
But now i want to use same program using switch statement.
i.e using switch case in place of if statement here...
can anyone please help...???
Thanks in advance
Re: Switch case ...interseting problem
The page on the switch statement in Oracle's Tutorial begins with a comparison of switch and a chain of if statements.
Post the code you are using with switch if you get stuck applying what's shown there.
Re: Switch case ...interseting problem
Quote:
Originally Posted by
pbrockway2
The page on the
switch statement in Oracle's Tutorial begins with a comparison of switch and a chain of if statements.
Post the code you are using with switch if you get stuck applying what's shown there.
ok i will try it ...
Thanks for Qick Support
Re: Switch case ...interseting problem
You're welcome.
Most people here don't like writing code for others, but if you have any problems at all with what's in the Tutorial you should post back because someone is sure to be able to help.
Re: Switch case ...interseting problem
These two snippets of code do exactly the same thing. Just follow this format and it is pretty easy. Just remember that if you don't add the break, it could execute more then one piece of the switch statement; and don't forget to add the default at the bottom.
Code:
String str;
System.out.println("Enter a number");
int var = keyboard.nextInt();
if(var == 1)
str = "one";
else if(var == 2)
str = "two";
else if(var == 3)
str = "three";
else if(var == 4)
str = "four";
else
str = "greater than four";
System.out.println(str);
Code:
String str;
System.out.println("Enter a number");
int var = keyboard.nextInt();
switch (var) {
case 1: str = "one";
break;
case 2: str = "two";
break;
case 3: str = "three";
break;
case 4: str = "four";
break;
default: str = "greater than four";
break;
}
System.out.println(str);
Re: Switch case ...interseting problem
Off topic stuff moved to /forum-lobby/.