내 프로그램에서는 주어진 문자 등급과 숫자 등급으로 GPA를 계산해야합니다. 첫 번째 대화 상자에서 취소를 클릭하면 numberFormatException null 오류가 발생하고 문자열을 입력하면 NumberFormatException 입력 문자열 오류가 발생합니다. 내 프로그램에서 시도 - 캐치를 사용하고 난 이미 어쨌든 나는 곤하는 법은 또한 사용자가 문자 성적 및 receptively 숫자 급 옵션 1과 2를 입력합니다 시도 - 캐치 모두 예외를 처리 할 수있다 it.Is을 시도 기타는 입니다. 다음자바, JOptionPane을 처리하는 방법 NumberFormatException 입력 문자열과 NumberFormatException : Null 오류?
는프로그램의 코드입니다
당신이하려고하지만, 시도/catch 블록 당신은 당신이 원하는만큼 예외를 포착하고 다르게 처리 할 수있는import javax.swing.JOptionPane;
class CalcGPA{
public static void main (String [] args){
String questionOfOptions = "What type of grade do you wish to enter?\n";
String letterGradeOption = "Press 1- for letter (A,C,etc)\n";
String numericGradeOption= "Press 2-for numeric(89,68,etc)";
String message= questionOfOptions
+letterGradeOption
+numericGradeOption;
String endingMessage = "The corresponding GPA is";
String calculatedGPA="0" ;
String randomString="kc;lc[ ams[xas";
String userResponseString="0";
int userResponseNumerical=0;
char characterValue='0';
do {
userResponseString=JOptionPane.showInputDialog
(null,
message,
"GPA Calculator",
JOptionPane.OK_CANCEL_OPTION);
try {
userResponseNumerical= Integer.parseInt(userResponseString);
} catch(NumberFormatException.forInputString ex) {
System.exit(0);
}
userContinuationOption= JOptionPane.showConfirmDialog
(null,
"Do You Want to Continue? ",
"GPA Calculator",
JOptionPane.YES_NO_OPTION);
if(userContinuationOption==JOptionPane.NO_OPTION) {
System.exit(JOptionPane.NO_OPTION);
}
switch(userResponseNumerical){
case 1:
String userGradeResponse = JOptionPane.showInputDialog
(null,
"Enter a Grade.",
"GPA Calculator",
JOptionPane.INFORMATION_MESSAGE);
if (userGradeResponse.equals("A")) {
calculatedGPA ="4.";
} else if (userGradeResponse.equals("B")) {
calculatedGPA=" 3.";
} else if (userGradeResponse.equals("C")) {
calculatedGPA=" 2.";
} else if (userGradeResponse.equals("D")) {
calculatedGPA=" 1.";
} else if(userGradeResponse.equals("F")) {
calculatedGPA=" 0.";
} else {
JOptionPane.showMessageDialog
(null,
"Improper input ",
"Error",
JOptionPane.ERROR_MESSAGE);
break;
}
JOptionPane.showMessageDialog
(null,
endingMessage+" "+calculatedGPA,
"GPA Calculator",
JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
String userNumericString= JOptionPane.showInputDialog
(null,
"Enter a Numeric Grade.",
"GPA Calculator",
JOptionPane.INFORMATION_MESSAGE);
int userNumericGradeResponse=0;
Integer.parseInt(userNumericString);
if(userNumericGradeResponse>=80) {
calculatedGPA=" 4.";
} else if (userNumericGradeResponse>70) {
calculatedGPA=" 3.";
} else if (userNumericGradeResponse>60) {
calculatedGPA=" 2.";
} else if (userNumericGradeResponse>50) {
calculatedGPA=" 1.";
} else if(userNumericGradeResponse>=0 &&
userNumericGradeResponse<50) {
calculatedGPA=" 0.";
}
break;
default:
JOptionPane.showMessageDialog
(null,
"Improper input",
"Error",
JOptionPane.INFORMATION_MESSAGE);
break;
}
int userContinuationOption = JOptionPane.showConfirmDialog
(null,
"Do You Want to Continue? ",
"GPA Calculator",
JOptionPane.YES_NO_OPTION);
if(userContinuationOption==JOptionPane.NO_OPTION) {
System.exit(JOptionPane.NO_OPTION);}
}
while(true);
}
}
다른 catch 블록에서 같은 일을합니까? 시스템 종료가 약간 가혹한 것 같지만. 수용? 그것은 거대한 큰 방법입니다! –