-2

I have a calculator that I've been working on for school.

It's not going to have a typical calculator GUI though.

I have a beta version where I use input boxes in order to take the input.

I was actually wondering if there was a way to make a function to close it and then reopen it sort of like a loop.

import javax.swing.JOptionPane;

public class JOptionPaneCalc {

public static void main(String [] args){

    char o = ' ';
    String input = " ";
    double num1 = 0;
    double num2 = 0;
    double result = 0;


    while (true){

    input = JOptionPane.showInputDialog("What is your operand? ");
    o = input.charAt(0); 

    if (o == '/') {
        result = num1 / num2;
        break;
    } else if (o == '*') {
        result = num1 * num2;
        break;
    } else if (o == '+') {
        result = num1 + num2;
        break;
    } else if (o == '-') {
        result = num1 - num2;
        break;
    }else if (o == 'q'){
        System.exit(0);
    }


    }

    input = JOptionPane.showInputDialog("What is your first number? ");
    num1 = Double.parseDouble(input);

    input = JOptionPane.showInputDialog("What is your second number? ");
    num2 = Double.parseDouble(input);

    if (o == '/') {
        result = num1 / num2;

    } else if (o == '*') {
        result = num1 * num2;

    } else if (o == '+') {
        result = num1 + num2;

    } else if (o == '-') {
        result = num1 - num2;

    }else if (o == 'q'){
        System.exit(0);
    }

    JOptionPane.showMessageDialog(null ,"Your answer is : " + result);


}   

}

This is what I have, hopefully you can help me

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Acara
  • 3
  • 3
  • 1
    what you mean by "closing and reopening it"? – Frakcool Apr 08 '14 at 17:05
  • You might want to check this post, which shows you how to programatically close a JOptionPane window. You could then re-open it once it is closed to deliver your output: http://stackoverflow.com/questions/18105598/closing-a-joptionpane-programatically – Nathaniel Payne Apr 08 '14 at 17:05
  • Its kind of hard to explain... I want it to finish executing it but then right before you press okay after it gives your the answer, to have a question if you want to do another problem rather than just close and have to rerun it over and over again. I can write the dialog box and all but i'm just not too sure on how to make it to reopen the program if the user specified yes. @Frakcool – Acara Apr 08 '14 at 17:07
  • ok, maybe you're trying to close the JOptionPane with `.dispose()` as @NathanielPayne told you – Frakcool Apr 08 '14 at 17:16

1 Answers1

1

There are quite a few issues with your code. If o == 'q', then your program will exit for good.

}else if (o == 'q'){
    System.exit(0);
}

Read about repetition. If you want your program to loop, then you should write it at least like this:

 public static void main(String[] args) {

    char o;
    String input;
    double num1;
    double num2;
    double result=0.;

    input = JOptionPane.showInputDialog("What is your operand? ");
    o = input.charAt(0);

    while (o!='q') {
        input = JOptionPane.showInputDialog("What is your first number? ");
        num1 = Double.parseDouble(input);
        input = JOptionPane.showInputDialog("What is your second number? ");
        num2 = Double.parseDouble(input);

        if (o == '/') {
            result = num1 / num2;
        } else if (o == '*') {
            result = num1 * num2;
        } else if (o == '+') {
            result = num1 + num2;
        } else if (o == '-') {
            result = num1 - num2;
        } 
        JOptionPane.showMessageDialog(null ,"Your answer is : " + result);

        input = JOptionPane.showInputDialog("What is your operand? ");
        o = input.charAt(0);
    }

}

Keep in mind that your users must be very sober, in order to avoid typos and receiving a NumberFormatException...

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47