私は学生で、Java の 2 週目です。宿題は、生徒の名前、ID、および 3 つのテストの点数について、キーボードからデータを取得することです。次に、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));
}
}