Newbie-W-if else class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Newbie-W-if else class

  1. #1
    Join Date
    Sep 2004
    Posts
    3

    Exclamation Newbie-W-if else class

    this is my second week dealing with java, i'm tryin to create a if else class thats calculats wages, displays hours worked and wage which are pre set but calculate the tax braket. here's what i came up with my self.

    class PayTime
    {

    static int hWage = 10;
    static int hWorked = 40;
    static int tPaid = hWage*hWorked;
    static int tTotal = 0;

    static void incr(){

    if (tPaid <= 300.00)
    tTotal = tPaid - ((tPaid/100)*10);
    System.out.println("Tax = ");
    System.out.println((tPaid/100)*10);
    System.out.println("Net Pay = ");
    System.out.println(tTotal);
    else if (tPaid >= 300.01)
    System.out.println("Tax = ");
    System.out.println((tPaid/100)*12);
    tTotal = tPaid - ((tPaid/100)*12);
    System.out.println("Net Pay = ");
    System.out.println(tTotal);
    else if (tPaid >= 400.01 & tPaid <= 500.00)
    System.out.println("Tax = ");
    System.out.println((tPaid/100)*15);
    tTotal = tPaid - ((tPaid/100)*15);
    System.out.println("Net Pay = ");
    System.out.println(tTotal);
    else
    System.out.println("Tax = ");
    System.out.println((tPaid/100)*20);
    tTotal = tPaid - ((tPaid/100)*20);
    System.out.println("Net Pay = ");
    System.out.println(tTotal); }

    public static void main(String[] args)
    {

    }
    }


    its a little messed so any help would be very much taken with eagerness.

  2. #2
    Join Date
    Oct 2004
    Posts
    11
    Okay, for starters, if you plan to execute multiple statements following an if or else, you need to surround said statements in braces.

    E.g.

    Code:
    if (someCondition) {
        doW();
        doX();
    } else {
        doY();
        doZ();
    }
    Your version looks more like this:

    Code:
    if (someCondition)
        doW();
        doX();
    else
        doY();
        doZ();
    In this case, doX() will happen regardless of the condition. If braces are omitted, only the statement immediately following the condition is conditional. Wrapping statements in braces makes the whole block conditional.

    In fact, this latter example would not compile at all, since the compiler would see that the "else" was detached from the "if" (separated by the non-conditional doX() method).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center