Results 1 to 8 of 8
- 08-15-2011, 11:01 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
I am having trouble with my switches.
I am very new to java and I am trying to make a very simple calculator. I have written my code from scratch but I can't get my switch to work. It keeps only calculating case 3 no matter what I tell it to do. Could anyone help me to realise where I have gone wrong?
Here is my code
PHP Code:import java.util.Scanner; public class Cal { public static void main(String[] args) { System.out.println("Enter first number"); Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); System.out.println("Enter second number"); Scanner dc = new Scanner(System.in); double b = dc.nextDouble(); System.out.println("Enter + or - or /"); Scanner ss1 = new Scanner(System.in); String s1 = ss1.next(); int switchy; if(s1.equals("/")); switchy=1; if(s1.equals("-")); switchy=2; if(s1.equals("+")); switchy=3; switch (switchy) { case 1: double h = a/b; System.out.println(h); break; case 2: h = a-b; System.out.println(h); break; case 3: h = a+b; System.out.println(h); break; } } }
- 08-15-2011, 11:30 PM #2
You forgot your brackets around your if statements. Also, don't forget to initialize switchy. You may want to catch the instance where someone enters something other than those symbols you want, or they enter a non number for your 1st two prompts. Try this:
Java Code:public class Cal { public static void main(String[] args) { System.out.println("Enter first number"); Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); System.out.println("Enter second number"); Scanner dc = new Scanner(System.in); double b = dc.nextDouble(); System.out.println("Enter + or - or /"); Scanner ss1 = new Scanner(System.in); String s1 = ss1.next(); int switchy = 0; if (s1.equals("/")){ switchy = 1; } if (s1.equals("-")){ switchy = 2; } if (s1.equals("+")){ switchy = 3; } switch (switchy) { case 1: double h = a / b; System.out.println(h); break; case 2: h = a - b; System.out.println(h); break; case 3: h = a + b; System.out.println(h); break; } } }
Last edited by sehudson; 08-15-2011 at 11:34 PM.
- 08-15-2011, 11:39 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
Thank you so much, I didnīt know I had to use brackets there. I now have a fully functioning basic calculator
- 08-16-2011, 12:16 AM #4
You don't have to use brackets. Your problem was you placed a semi-colon after the if statement, thus terminating it. So regardless of whether the if statements were true or not you always set switchy to 1 then 2 and finally 3.
By the way why are you using an if statement and a switch statement? Why not just move the code from your switch cases into the if statements?
- 08-16-2011, 04:14 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 14
- Rep Power
- 0
Brackets is not mandatory. If your IF condition contains more 1 statements then you need to use brackets.
For and example
puclic String test(int value)
{
if (value = 0)
return 'Zero';
if (value <0)
{
value ++;
return 'more than zero'
}
}
- 08-16-2011, 04:41 AM #6
Senior Member
- Join Date
- Aug 2011
- Posts
- 251
- Rep Power
- 7
- 08-27-2011, 01:55 AM #7
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
Thanks so much. All this information has been very useful to me
- 08-27-2011, 02:32 AM #8
Similar Threads
-
Compiling with -d -cp switches
By codeAJ in forum New To JavaReplies: 8Last Post: 06-05-2011, 03:25 AM -
Using switches...
By besweeet in forum New To JavaReplies: 11Last Post: 03-04-2010, 04:48 PM -
help with switches
By spots of fire in forum New To JavaReplies: 1Last Post: 01-23-2010, 09:02 PM -
how to convert from switches to methods
By peacehope in forum New To JavaReplies: 4Last Post: 03-27-2009, 12:33 AM -
Help with switches
By Daniel in forum New To JavaReplies: 2Last Post: 07-04-2007, 08:37 AM
Bookmarks