Results 1 to 6 of 6
- 10-09-2012, 07:26 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 14
- Rep Power
- 0
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
- 10-09-2012, 08:26 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
- 10-09-2012, 08:32 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 14
- Rep Power
- 0
- 10-09-2012, 08:36 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
- 10-09-2012, 08:44 AM #5
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
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.
Java 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);Java 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);Last edited by penguinCoder; 10-09-2012 at 05:18 PM. Reason: added curly brace
- 10-11-2012, 10:05 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Similar Threads
-
about conditonal if else and switch case
By niksipandit in forum New To JavaReplies: 6Last Post: 09-22-2011, 11:53 AM -
java switch case
By aconti in forum New To JavaReplies: 16Last Post: 08-09-2011, 07:05 AM -
Converting if to Switch Case.
By king2be98 in forum New To JavaReplies: 5Last Post: 02-20-2011, 03:46 PM -
Switch Case statement
By seanfmglobal in forum New To JavaReplies: 7Last Post: 02-15-2011, 01:18 PM -
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 05:56 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks