Conditional logic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blknav01
    New Member
    • Sep 2007
    • 1

    Conditional logic

    Greetings all. I am having a problem with getting java program to produce the correct output. I am being asked to use conditional logic to determine a tax rate category. I am not getting any syntax errors at all but I am not getting the correct code after putting in the annual salary as directed.
    Here is my code. Any help in pointing me in the right direction will be greatly appreciated.


    import javax.swing.JOp tionPane;

    public class TaxCodeTest1 {
    public static void main(String[] args) {


    float salary = 0;
    float code = 0;

    JOptionPane.sho wInputDialog(nu ll,
    "Enter annual salary, example 10000",
    "Input",
    JOptionPane.QUE STION_MESSAGE);



    if (salary < 15000) { //Salary less than 15000 code 1
    code = '1';
    JOptionPane.sho wMessageDialog( null, " Tax Code =" + 1);
    }
    else if (salary >= 15001 - 25000) { //salary between 15001 - 25000 code 2
    code = '2';
    JOptionPane.sho wMessageDialog( null, " Tax Code =" + 2);
    }
    else if (salary >= 25001 - 40000) { //salary between 25001 - 40000 code 3
    code = '3';
    }
    else if (salary >= 40001 - 65000) { //salary between 40001 - 65000
    code = '4';
    }
    else if (salary >= 65001 - 80000) { //salary between 65001 - 80000
    code = '5';
    }
    else if (salary > 80000) { //salary greater than 80000
    code = '6';


    JOptionPane.sho wMessageDialog( null, "The code based on annual salary") ;

    }
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You can't test whether or not a value lies in an interval that way: the '-' symbol
    actually means subtraction so the compiler (that doesn't know what you want to
    do) compiles your code but your compiled code does quite something different
    from what you want to accomplish.

    If you want to check whether or not a value 'v' lies in an interval 'lo' ... 'hi' do this:

    [code=java]
    if (lo <= v && v < hi) ...
    [/code]

    The interval is 'half open' i.e. the interval is [lo, hi)

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by blknav01
      Greetings all. I am having a problem with getting java program to produce the correct output. I am being asked to use conditional logic to determine a tax rate category. I am not getting any syntax errors at all but I am not getting the correct code after putting in the annual salary as directed.
      Here is my code. Any help in pointing me in the right direction will be greatly appreciated.


      import javax.swing.JOp tionPane;

      public class TaxCodeTest1 {
      public static void main(String[] args) {


      float salary = 0;
      float code = 0;

      JOptionPane.sho wInputDialog(nu ll,
      "Enter annual salary, example 10000",
      "Input",
      JOptionPane.QUE STION_MESSAGE);



      if (salary < 15000) { //Salary less than 15000 code 1
      code = '1';
      JOptionPane.sho wMessageDialog( null, " Tax Code =" + 1);
      }
      else if (salary >= 15001 - 25000) { //salary between 15001 - 25000 code 2
      code = '2';
      JOptionPane.sho wMessageDialog( null, " Tax Code =" + 2);
      }
      else if (salary >= 25001 - 40000) { //salary between 25001 - 40000 code 3
      code = '3';
      }
      else if (salary >= 40001 - 65000) { //salary between 40001 - 65000
      code = '4';
      }
      else if (salary >= 65001 - 80000) { //salary between 65001 - 80000
      code = '5';
      }
      else if (salary > 80000) { //salary greater than 80000
      code = '6';


      JOptionPane.sho wMessageDialog( null, "The code based on annual salary") ;

      }
      }
      }
      1) Use code tags when posting code.
      2.) Your salary is always 0. You did not assign the value entered to the salary variable.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        You can't test whether or not a value lies in an interval that way: the '-' symbol
        actually means subtraction so the compiler (that doesn't know what you want to
        do) compiles your code but your compiled code does quite something different
        from what you want to accomplish.

        If you want to check whether or not a value 'v' lies in an interval 'lo' ... 'hi' do this:

        [code=java]
        if (lo <= v && v < hi) ...
        [/code]

        The interval is 'half open' i.e. the interval is [lo, hi)

        kind regards,

        Jos
        Hey, haven't we seen this code before?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          Hey, haven't we seen this code before?
          Yup, l'histoire se repete as the Germans say ;-)

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            Yup, l'histoire se repete as the Germans say ;-)

            kind regards,

            Jos
            I prefer king Solomon's "There is nothing new under the sun".

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              I prefer king Solomon's "There is nothing new under the sun".
              As in "been there; done that"? ;-)

              kind regards,

              Jos

              Comment

              Working...