gpt4 book ai didi

java - 从 JOptionPane.showInputDialog 框中读取输入

转载 作者:行者123 更新时间:2023-12-02 06:22:46 24 4
gpt4 key购买 nike

我快完成这个项目了。这是我的第一个,我需要更好地阅读 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
}

最佳答案

您的问题是,如果用户单击“取消”,operationType 将为 null,从而引发 NullPointerException。我建议你搬家

if (operationType.equalsIgnoreCase("Q"))

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

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

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

然后,将其余所有 if 更改为 else if。这样,一旦程序看到输入是否为空,它就不会尝试在操作类型上调用任何其他内容。这样做的另一个好处是使其更加高效 - 一旦程序发现输入是其中一个选项,它就不会费心将其与其余选项进行检查。

关于java - 从 JOptionPane.showInputDialog 框中读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com