Java 如果有效整数不是',我如何让程序以错误终止;你没进去吗?
对于学校,我正在制作一个程序,用户输入三个整数,程序找到这三个整数的乘积,并将结果输出给用户。老师要求我使用JOptionPane类。输入无效整数时,如何使程序以错误终止。还有,如何在java窗口中输出答案?提前谢谢Java 如果有效整数不是',我如何让程序以错误终止;你没进去吗?,java,dialog,Java,Dialog,对于学校,我正在制作一个程序,用户输入三个整数,程序找到这三个整数的乘积,并将结果输出给用户。老师要求我使用JOptionPane类。输入无效整数时,如何使程序以错误终止。还有,如何在java窗口中输出答案?提前谢谢 import javax.swing.JOptionPane; public class ASTheProductofThreeGUI { public static void main(String[] args) { //initializes v
import javax.swing.JOptionPane;
public class ASTheProductofThreeGUI {
public static void main(String[] args) {
//initializes variable "answer" of type integer
//prompts the user to enter their first integer for the product of three
int value1 = Integer.parseInt(JOptionPane.showInputDialog("Enter your first"
+ " value as an integer"));
//prompts the user to enter their second integer
int value2 = Integer.parseInt(JOptionPane.showInputDialog("Enter your second"
+ " value as an integer"));
//prompts the user to enter their third integer
int value3 = Integer.parseInt(JOptionPane.showInputDialog("Enter your third"
+ " value as an integer"));
int answer = value1 * value2 * value3;
如果字符串不是有效的整数,Integer.parseInt将引发异常。您应该处理NumberFormatException,然后调用System.exit
import javax.swing.JOptionPane;
public class ASTheProductofThreeGUI {
public static void main(String[] args) {
//initializes variable "answer" of type integer
//prompts the user to enter their first integer for the product of three
try {
int value1 = Integer.parseInt(JOptionPane.showInputDialog("Enter your first"
+ " value as an integer"));
//prompts the user to enter their second integer
int value2 = Integer.parseInt(JOptionPane.showInputDialog("Enter your second"
+ " value as an integer"));
//prompts the user to enter their third integer
int value3 = Integer.parseInt(JOptionPane.showInputDialog("Enter your third"
+ " value as an integer"));
catch (NumberFormatException e){
e.printStackTrace("Invalid Integer entered.");
System.exit(0);
}
int answer = value1 * value2 * value3;
同样,作为问题询问家庭作业也是不受欢迎的,因此我将留下您的第二个问题让您去了解。如果字符串不是有效的整数,Integer.parseInt将引发异常。您应该处理NumberFormatException,然后调用System.exit
import javax.swing.JOptionPane;
public class ASTheProductofThreeGUI {
public static void main(String[] args) {
//initializes variable "answer" of type integer
//prompts the user to enter their first integer for the product of three
try {
int value1 = Integer.parseInt(JOptionPane.showInputDialog("Enter your first"
+ " value as an integer"));
//prompts the user to enter their second integer
int value2 = Integer.parseInt(JOptionPane.showInputDialog("Enter your second"
+ " value as an integer"));
//prompts the user to enter their third integer
int value3 = Integer.parseInt(JOptionPane.showInputDialog("Enter your third"
+ " value as an integer"));
catch (NumberFormatException e){
e.printStackTrace("Invalid Integer entered.");
System.exit(0);
}
int answer = value1 * value2 * value3;
同样,作为一个问题问家庭作业也是不被接受的,所以我将留下你的第二个问题让你去发现。smokeyrobot,让我扩展你的建议,并给Samuel一个关于干净编码的提示。正如Robert C.Martin在他的优秀著作《干净的代码》中所说的那样,try/catch块是丑陋的,它混淆了错误处理与正常处理混合的代码结构。下面是我对塞缪尔问题的建议:
public class JOptionPaneExample {
public static void main(String[] args){
try {
productOfThreeNumbers();
} catch (NumberFormatException e) {
e.printStackTrace();
showErrorDialog();
System.exit(0);
}
}
private static void productOfThreeNumbers() {
//initializes variable "answer" of type integer
//prompts the user to enter their first integer for the product of three
int value1 = Integer.parseInt(JOptionPane.showInputDialog("Enter your first"
+ " value as an integer"));
//prompts the user to enter their second integer
int value2 = Integer.parseInt(JOptionPane.showInputDialog("Enter your second"
+ " value as an integer"));
//prompts the user to enter their third integer
int value3 = Integer.parseInt(JOptionPane.showInputDialog("Enter your third"
+ " value as an integer"));
int answer = value1 * value2 * value3;
JOptionPane.showMessageDialog(null, String.format("%d * %d * %d = %d", value1, value2, value3, answer));
}
private static void showErrorDialog() {
//Put here code to show a friendly error message...
}}
smokeyrobot,让我扩展你的建议,并给Samuel一个关于干净编码的提示。正如Robert C.Martin在他的优秀著作《干净的代码》中所说的那样,try/catch块是丑陋的,它混淆了错误处理与正常处理混合的代码结构。下面是我对塞缪尔问题的建议:
public class JOptionPaneExample {
public static void main(String[] args){
try {
productOfThreeNumbers();
} catch (NumberFormatException e) {
e.printStackTrace();
showErrorDialog();
System.exit(0);
}
}
private static void productOfThreeNumbers() {
//initializes variable "answer" of type integer
//prompts the user to enter their first integer for the product of three
int value1 = Integer.parseInt(JOptionPane.showInputDialog("Enter your first"
+ " value as an integer"));
//prompts the user to enter their second integer
int value2 = Integer.parseInt(JOptionPane.showInputDialog("Enter your second"
+ " value as an integer"));
//prompts the user to enter their third integer
int value3 = Integer.parseInt(JOptionPane.showInputDialog("Enter your third"
+ " value as an integer"));
int answer = value1 * value2 * value3;
JOptionPane.showMessageDialog(null, String.format("%d * %d * %d = %d", value1, value2, value3, answer));
}
private static void showErrorDialog() {
//Put here code to show a friendly error message...
}}