从JOptionPane.showInputDialog框中读取输入

时间:2013-12-31 22:25:41

标签: java swing modal-dialog joptionpane cancel-button

我差不多完成了这个项目。这是我的第一个,我需要更好地阅读JOption框中的输入。例如,如果用户单击“取消”或关闭程序,我想返回上一页。到目前为止,每次我点击“取消”我的程序崩溃。帮帮我!我刚刚开始学习Java一个月前。我想我做得很好。 非常感谢!

这是代码,

import java.util.Scanner;
import javax.swing.JOptionPane;

/**
 * Write a description of class Operation here.
 *
 * @author Charles Kossivi
 * @version 1.0
 */
public class Operation {

    static String firstNumber; //first number or coefficient
    static String secondNumber; //second number or coefficient
    static String thirdNumber; //third number or coefficient
    static String operationType = ""; //Operation type
    static boolean anotherOperation = false;
    static boolean goodType = true; //true=run program false=stop program
    static boolean numberTypeA = true; // another operation?
    static boolean numberTypeB = true; // another operation? 
    static boolean numberTypeC = true; // another operation? 
    static Scanner keyInput = new Scanner(System.in);
    static String inputType; // temperature type
    static double a; // first number
    static double b; // second number
    static double c; // third number
    static double s; // sum of a and b
    static double d; // difference of a and b
    static double p; // product of a and b
    static double r; // ratio of a and b
    static double D; // discriminant
    static double numType;

    public static void main(String[] args) {
        while (goodType) {
            operationType = JOptionPane.showInputDialog("Welcome! \nPlease, Select the Desired Operation Type"
                    + "(\nA=Addition, \nD=Division, \nE=Quadratic Equation, "
                    + "\nM=Multiplication, \nS=Subtraction, \nQ=Quit");
            if (operationType.equalsIgnoreCase("A")) {
                newAddition();
            }
            if (operationType.equalsIgnoreCase("S")) {
                newSubtraction();
            }
            if (operationType.equalsIgnoreCase("M")) {
                newMultiplication();
            }
            if (operationType.equalsIgnoreCase("D")) {
                newDivision();
            }
            if (operationType.equalsIgnoreCase("E")) {
                newQuadratic();
            }
            if (operationType.equalsIgnoreCase("Q")) {
                // quit   
                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
                goodType = false;
            }
        }
        goodType = false;
    }

    //Start Addition class here
    private static void newAddition() {
        //read in first coefficient from user as a string
        firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");
        if (firstNumber.equalsIgnoreCase("Q")) {
            // quit   
            JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
            goodType = false;
        } else {
            a = Double.parseDouble(firstNumber);
            //read in second coefficient from user as a string
            secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");
            if (secondNumber.equalsIgnoreCase("Q")) {
                // quit   
                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
                goodType = false;
            } else {
                b = Double.parseDouble(secondNumber);
                //Adding the numbers
                s = a + b;
                //Display results
                JOptionPane.showMessageDialog(null, "The sum is " + s);
            }
        }
    }

    //End Addition class
    //Start Addition class here
    private static void newSubtraction() {
        //read in first coefficient from user as a string
        firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");
        if (firstNumber.equalsIgnoreCase("Q")) {
            // quit   
            JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
            goodType = false;
        } else {
            a = Double.parseDouble(firstNumber);
            //read in second coefficient from user as a string
            secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");
            if (secondNumber.equalsIgnoreCase("Q")) {
                // quit   
                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
                goodType = false;
            } else {
                b = Double.parseDouble(secondNumber);
                //Subtraction the numbers
                d = a - b;
                //Display results
                JOptionPane.showMessageDialog(null, "The sum is " + d);
            }
        }
    }//End Subtraction class

    // Begin Multiplication class
    private static void newMultiplication() {
        //read in first coefficient from user as a string
        firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");
        if (firstNumber.equalsIgnoreCase("Q")) {
            // quit   
            JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
            goodType = false;
        } else {
            a = Double.parseDouble(firstNumber);
            //read in second coefficient from user as a string
            secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");
            if (secondNumber.equalsIgnoreCase("Q")) {
                // quit   
                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
                goodType = false;
            } else {
                b = Double.parseDouble(secondNumber);
                //multiplying the numbers
                p = a * b;
                //Display results
                JOptionPane.showMessageDialog(null, "The product is " + p);
            }
        }
    } // End Multiplication class

    //Start Division class
    private static void newDivision() {
        //read in first coefficient from user as a string
        firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");
        if (firstNumber.equalsIgnoreCase("Q")) {
            // quit   
            JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
            goodType = false;
        } else {
            a = Double.parseDouble(firstNumber);
            //read in second coefficient from user as a string
            secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");
            if (secondNumber.equalsIgnoreCase("Q")) {
                // quit   
                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
                goodType = false;
            } else {
                b = Double.parseDouble(secondNumber);
                //Dividing the numbers
                r = a / b;
                //Display results
                JOptionPane.showMessageDialog(null, "The ratio is " + r);
            }
        }
    } //End Division class

    //Start Quadratic class
    private static void newQuadratic() {
        //read in first coefficient from user as a string
        firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");
        if (firstNumber.equalsIgnoreCase("Q")) {
            // quit   
            JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
            goodType = false;
        } else {
            a = Double.parseDouble(firstNumber);
            //read in second coefficient from user as a string
            secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");
            if (secondNumber.equalsIgnoreCase("Q")) {
                // quit   
                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
                goodType = false;
            } else {
                b = Double.parseDouble(secondNumber);
                //read in third coefficient from user as a string
                thirdNumber = JOptionPane.showInputDialog("Please, Enter Third coefficient");
                if (thirdNumber.equalsIgnoreCase("Q")) {
                    // quit   
                    JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");
                    goodType = false;
                } else {
                    c = Double.parseDouble(thirdNumber);
                    //Quadratic formula
                    D = (Math.sqrt(b * b - 4 * a * c)) / (2 * a);
                    //Display results
                    if (D >= 0) {
                        JOptionPane.showMessageDialog(
                                null, "The equation's first real root is " + ((-b + D) / (2 * a)) + "\n"
                                + "The equation's second real root is " + ((-b - D) / (2 * a)) + "\n");
                    } else {
                        JOptionPane.showMessageDialog(
                                null, "The equation has no real solution");
                    }
                }
            }
        }
    }// end class quadratics
}

1 个答案:

答案 0 :(得分:2)

您的问题是,如果用户单击取消,operationType为空,从而抛出NullPointerException。我建议你搬家

if (operationType.equalsIgnoreCase("Q"))

到if语句组的开头,然后将其更改为

if(operationType==null||operationType.equalsIgnoreCase("Q")).

这将使程序退出,就像用户在按下取消按钮时选择了退出选项一样。

然后,将所有其余的ifs更改为else ifs。这样,一旦程序看到输入是否为null,它就不会尝试在operationType上调用任何其他内容。这样做的另一个好处是可以提高效率 - 一旦程序看到输入是其中一个选项,它就不会费心去检查它们。

相关问题