Results 1 to 2 of 2

Thread: sum value

  1. #1
    acat is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default sum value

    Hi, guys again
    Need you help again!
    if I want sum of the beforeprice and discountprice respectively, how could I write!

    public class radiobutton implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    String prcie_String = e.getActionCommand ();


    if (prcie_String.compareTo ("food 1")==0 )
    { double beforeprice = 23.00;
    double discountprice = beforeprice * 0.9;

    output1.setText("",beforeprice);
    output2.setText("",discountprice);
    }
    else if (prcie_String.compareTo ("food2")==0 )
    {
    double price = 33.00;
    double discountprice = beforeprice * 0.9;

    output1.setText("",beforeprice);
    output2.setText("",discountprice);
    }
    else if (prcie_String.compareTo ("food3")==0 )
    {
    double price = 53.00;
    double discountprice = beforeprice * 0.9;

    output1.setText("",beforeprice);
    output2.setText("",discountprice); }
    }
    Last edited by acat; 04-11-2011 at 07:57 AM.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You're not going to be able to accomplish this in If/ElseIf/Else statements because only one of them will trigger at a time. Since you're setting the variables inside the If/ElseIf/Else statements the variable price will only hold one value per instance.

    You could do something like this:
    Java Code:
    int price1 = 23;
    int price2 = 33;
    int price3 = 53;
    
    if (prcie_String.compareTo ("price 1")==0 )
    {
    output.setText("",price1);
    }
    else if (prcie_String.compareTo ("price 2")==0 )
    {
    output.setText("",price2);
    }
    else if (prcie_String.compareTo ("price 3")==0 )
    {
    output.setText("",price3);
    }
    int total = price1 + price2 + price3;

Posting Permissions

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