Results 1 to 3 of 3

Thread: calculator

  1. #1
    oltaA is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default calculator

    im working for a calculator using eclipse. for the "=" button ive written the code:
    JButton btn_result = new JButton("=");
    btn_result.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    secondDouble=(Double.parseDouble(String.valueOf(di splay.getText())));
    if(plusClick>0){
    totalDouble=firstDouble+secondDouble;
    display.setText(String.valueOf(totalDouble));
    firstDouble=0;
    secondDouble=0;
    plusClick=0;


    }
    if(minusClick>0){
    totalDouble=firstDouble-secondDouble;
    display.setText(String.valueOf(totalDouble));
    firstDouble=0;
    secondDouble=0;
    minusClick=0;

    }
    if(multiplyClick>0){
    totalDouble=firstDouble*secondDouble;
    display.setText(String.valueOf(totalDouble));
    firstDouble=0;
    secondDouble=0;
    multiplyClick=0;

    }
    if(divideClick>0){
    totalDouble=firstDouble/secondDouble;
    display.setText(String.valueOf(totalDouble));
    firstDouble=0;
    secondDouble=0;
    divideClick=0;
    }
    if(percentageClick>0){
    totalDouble=(firstDouble * 100/ secondDouble);
    display.setText(String.valueOf(totalDouble));
    firstDouble=0;
    secondDouble=0;
    percentageClick=0;
    }
    System.out.println("In ActionPerformed");
    the problem is: i want to repeat this action for any click of the "=" ... ex. 8-3 click=, result 5. now i click again= and i wont the result 2. like the microsoft calculator... help pleaasee :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: calculator

    When posting code, please use the highlight tags to preserve formatting.

    Sounds to me like you'll have to keep track of the last action taken. Without an SSCCE I'm just guessing though.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: calculator

    Please place your code between tags. Also, you need to provide an SSCCE that demonstrates the problem.
    Where do you handle the equals sign? What are the other conditionals for and how are they set?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. need some help with my calculator
    By Tooks1 in forum New To Java
    Replies: 0
    Last Post: 02-13-2013, 07:38 PM
  2. Need help with + - * / calculator
    By Aconitum in forum New To Java
    Replies: 26
    Last Post: 08-04-2012, 08:13 PM
  3. calculator help
    By sirstroud in forum New To Java
    Replies: 0
    Last Post: 04-01-2012, 06:58 AM
  4. Need help with Calculator
    By Joshua4missions in forum New To Java
    Replies: 4
    Last Post: 12-10-2011, 03:44 AM
  5. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 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
  •