Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Junky

Thread: if, else if, else statement

  1. #1
    aragornfin is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default if, else if, else statement

    so I needed to insert this or something similar into the code below it.. can someone help me figure out what I am doing wrong?
    {
    if (sales => 100,000)
    {
    then sales * 0.08
    }
    else If (sales > 120,000)
    {
    then sales * 0.08 * 1.25
    }
    else (sales < 100,000)
    {
    then sales * 0.00
    }

    /*need to insert into this code
    Revised coding part 1
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package totalcompensationw3;

    import java.util.Scanner;
    /**
    *
    * @author Aragorn
    */
    public class TotalCompensationW3
    {
    private double fixedSalary;
    private double calculatedCommission;

    /**
    * @param args the command line arguments
    */
    public void main(String[] args)
    {
    {
    fixedSalary = 0;
    calculatedCommission = 0;
    }
    }
    public void Compensation(double base, double comm)
    {
    fixedSalary = base;
    calculatedCommission = comm;
    }
    public void setSalary(double salary)
    {
    fixedSalary = salary;
    }
    public void setCommission(double commission)
    {
    calculatedCommission = commission;
    }
    public double getSalary()
    {
    return fixedSalary;
    }
    public double getCommission()
    {
    return calculatedCommission;
    }
    public double calculateWeek2Commission(double sales)
    {
    return (sales * 0.08);
    }
    public double getTotalCompensation()
    {
    return (fixedSalary + calculatedCommission);
    }
    }

    Revised coding part 2
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package totalcompensationw3;

    import java.util.Scanner;

    /**
    *
    * @author Aragorn
    */
    public class compensationCalculation
    {
    /**
    * @param args the command line arguments
    */
    public static void main ( String[] args)
    {
    TotalCompensationW3 myComp = new TotalCompensationW3(); // Instantiate the compensation class
    double sales = 0; // initialize the sales revenue value
    Scanner keyboard = new Scanner(System.in);
    try
    {
    System.out.println("Enter the Sales Revenue: ");
    sales = keyboard.nextInt(); // Ask user for Sales Revenue
    myComp.setSalary(30000); // Set the base salary to $30,000
    myComp.setCommission(myComp.calculateWeek2Commissi on(sales)); // Calculate the compensation
    System.out.println("The Sales Revenue is: " + sales); // Output Sales Revenue
    System.out.println("The Base Salary is: " + myComp.getSalary()); // Output the Base Salary
    System.out.println("The Commission is: " + myComp.getCommission()); // Output the Commission
    System.out.println("The Total Annual Compensation is: " + myComp.getTotalCompensation()); // Output the Total Annual Compensation
    } catch (Exception e)
    {
    System.out.println(e.toString()); // In case an error occurs, send error message to screen
    }
    }
    }

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

    Default Re: if, else if, else statement

    Java Code:
    if(condition) {
    
    } else if (condition) {
    
    } else {
    
    }
    Can you see what is wrong with your code?
    Kagiso likes this.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: if, else if, else statement

    Please use [code] tags [/code] when posting code.
    Unformatted code is hard to follow.
    Please do not ask for code as refusal often offends.

  4. #4
    aragornfin is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: if, else if, else statement

    Junky,
    I do see the difference between the code. which levees me scratching my head, but how would I incorporate or insert the code into the main program? would I creates a new program using a variable for the 0.08 commission in the original program?

  5. #5
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: if, else if, else statement

    you can't say if...then within a java code(is this a pseudocode ?) perhaps you wanted to say:
    {
    if (sales => 100,000)
    {
    sales *= 0.08;
    }
    else if (sales > 120,000)
    {
    sales *=( 0.08 * 1.25); // I would use parenthesis to avoid confusion
    }
    else (sales < 100,000)
    {
    sales *= 0.00; // which gives you a zero.... is this what you want?.... to assign a zero to this variable
    }

    i hope this will help you...

  6. #6
    aragornfin is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: if, else if, else statement

    so I am no closer to getting the code to run than when I started. what changes do I need to make to the original code and what should the new code be?

Similar Threads

  1. If statement help
    By Manny123 in forum New To Java
    Replies: 5
    Last Post: 10-28-2012, 12:30 AM
  2. if statement
    By Exceedinglife in forum New To Java
    Replies: 1
    Last Post: 04-25-2012, 01:25 AM
  3. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  4. If statement, please help??
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 11-23-2008, 02:58 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

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