Results 1 to 8 of 8
  1. #1
    Louby85 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Question 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;
    
    
    
    }
    }
    }
    Thanks in advance.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    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.

  3. #3
    Louby85 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you so much, I didnīt know I had to use brackets there. I now have a fully functioning basic calculator

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Louby85 View Post
    Thank you so much, I didnīt know I had to use brackets there. I now have a fully functioning basic calculator
    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?

  5. #5
    chinna is offline Member
    Join Date
    Jul 2011
    Posts
    14
    Rep Power
    0

    Default

    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'
    }

    }

  6. #6
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default

    Quote Originally Posted by sehudson View Post
    You forgot your brackets around your if statements.
    If you got just one statement you dont need brackets.

  7. #7
    Louby85 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks so much. All this information has been very useful to me

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    If you always use brackets you rarely have the problem. Make it a habit to add the opening { and the closing } on the next line

Similar Threads

  1. Compiling with -d -cp switches
    By codeAJ in forum New To Java
    Replies: 8
    Last Post: 06-05-2011, 03:25 AM
  2. Using switches...
    By besweeet in forum New To Java
    Replies: 11
    Last Post: 03-04-2010, 04:48 PM
  3. help with switches
    By spots of fire in forum New To Java
    Replies: 1
    Last Post: 01-23-2010, 09:02 PM
  4. how to convert from switches to methods
    By peacehope in forum New To Java
    Replies: 4
    Last Post: 03-27-2009, 12:33 AM
  5. Help with switches
    By Daniel in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •