我是一名学生,这是我学习 Java 的第二周。作业是从键盘获取数据,包括学生姓名、ID 和三个考试成绩。然后使用 JOptionPane 显示主要数据。我相信我已经完成了所有这些。我已经把作业做得更远了,这样我也可以了解单元测试。
问题是 ID 和测试分数应该是数字。如果输入非数字值,我会收到 IOExceptions。我想我需要使用 try/catch,但到目前为止我所看到的一切都让我感到困惑。有人可以分解 try/catch 的工作原理以便我理解吗?
//Import packages
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
*
* @author Kevin Young
*/
public class StudentTestAverage {
//A reusable method to calculate the average of 3 test scores
public static double calcAve(double num1, double num2, double num3){
final double divThree = 3;
return (num1 + num2 + num3 / divThree);
}
//A method to turn a doule into an integer
public static int trunAve(double num1){
return (int) num1;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
//Input variables
String strStudentName = "";
int intStudentID = 0;
double dblScore1 = 0.0;
double dblScore2 = 0.0;
double dblScore3 = 0.0;
String strNumber = ""; //Receives a string to be converted to a number
//Processing variables
double dblAverage = 0.0;
int intAverage = 0;
/**
* Create objects that read keyboard data from a buffer
*/
//Create the reader and Buffer the input stream to form a string
BufferedReader brObject =
new BufferedReader(new InputStreamReader(System.in));
//Get the student's name
do{
System.out.print("Please enter the student's name?");
strStudentName = brObject.readLine();
}while(strStudentName.equals(""));
//Use the scanner to get the student ID
//this method converts the string to an Integer
Scanner scan = new Scanner(System.in);
do{
System.out.print("Please enter the student's ID?");
intStudentID = scan.nextInt();
}while(Double.isNaN(intStudentID));
/*
* The above do while loop with the Scanner isn't working as
* expected. When non-numeric text is entered it throws an
* exception. Has the same issue when trying to use parseInt().
* Need to know how to handle exceptions.
*/
/**
* Us JOption to get string data and convert it to a double
*/
do{
strNumber = JOptionPane.showInputDialog("Please enter the first test score?");
dblScore1 = Double.parseDouble(strNumber);
}while(Double.isNaN(dblScore1));
do{
strNumber = JOptionPane.showInputDialog("Please enter the second test score?");
dblScore2 = Double.parseDouble(strNumber);
}while(Double.isNaN(dblScore2));
do{
strNumber = JOptionPane.showInputDialog("Please enter the third test score?");
dblScore3 = Double.parseDouble(strNumber);
}while(Double.isNaN(dblScore3));
//Calculate the average score
dblAverage = calcAve(dblScore1, dblScore2, dblScore3);
//Truncate dblAverage making it an integer
intAverage = trunAve(dblAverage);
/**
* Display data using the JOptionPane
*/
JOptionPane.showMessageDialog(
null, "Student " + strStudentName + " ID " +
Integer.toString(intStudentID) + " scored " +
Double.toString(dblScore1) + ", " +
Double.toString(dblScore2) + ", and " +
Double.toString(dblScore3) + ".\n For an average of " +
Double.toString(dblAverage));
//Output the truncated average
System.out.println(Integer.toString(intAverage));
}
}