所以我試圖在我的程式中實作錯誤捕獲器,但我不是 100% 確定它們實際上是如何作業的,我在 YouTube 上看過一些教程,但他們向我解釋的方式并不是 100% 清楚,所以我'我實際上在這里嘗試做的是嘗試在代碼的末尾添加一些東西,如果你插入一個他無法識別的字母或數字,他不會給我們一個錯誤,但他會給我們一個訊息,告訴我們什么出錯。
我試圖用 if 陳述句制作一個更簡單的版本,但我認為這并不完全適合這段代碼,因為我必須用 if 陳述句重寫所有內容,而且我不確定實際是如何作業的。
我認為可以作業的代碼
它不允許我把代碼放在這里:C
我試圖實作錯誤處理的代碼
import java.util.Scanner;
公共類主{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Insert number of students: ");
String student = sc.nextLine();
int studenti = Integer.parseInt(student);
int[] array = new int[studenti];
for (int i = 0; i<studenti;i )
{
System.out.println("Insert a score in percentage: ");
String score = sc.nextLine();
array[i]= Integer.parseInt(score);
}
System.out.println("\nPercentages are: ");
int sum = 0; // Added a variable for sum
for (int i = 0; i < studenti; i ) {
System.out.println((array[i]) "%");
sum = array[i]; // maintaining sum
}
int average = sum / array.length; // calculating the average
System.out.println("\nThe Average Score is: " average "%");
uj5u.com熱心網友回復:
使用 try/catch 塊。
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Insert number of students: ");
String student = sc.nextLine();
try{
int studenti = Integer.parseInt(student);
int[] array = new int[studenti];
for (int i = 0; i<studenti;i )
{
System.out.println("Insert a score in percentage: ");
String score = sc.nextLine();
array[i]= Integer.parseInt(score);
}
System.out.println("\nPercentages are: ");
int sum = 0; // Added a variable for sum
for (int i = 0; i < studenti; i ) {
System.out.println((array[i]) "%");
sum = array[i]; // maintaining sum
}
int average = sum / array.length; // calculating the average
System.out.println("\nThe Average Score is: " average "%");
}catch(Exception ex){
System.out.println("Error, details: " ex.Message());
}
您可以使用許多“例外”,例如 numberFormatException。
uj5u.com熱心網友回復:
錯誤處理并沒有那么復雜,真的。try它是關于知道在陳述句的范圍內拋出了什么樣的例外。當您將游標放在正在使用的方法上時,IDE 通常可以使用此資訊。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("Insert number of students: ");
int student = sc.nextInt(); // note Im using a method specific for int, instead of parsing
int[] array = new int[student];
for (int i = 0; i < student; i ) {
System.out.println("Insert a score in percentage: ");
String score = sc.nextLine();
array[i]= Integer.parseInt(score);
}
System.out.println("\nPercentages are: ");
int sum = 0;
for (int i = 0; i < student; i ) {
System.out.println((array[i]) "%");
sum = array[i];
}
int average = sum / student; // student == array.length
System.out.println("\nThe Average Score is: " average "%");
} catch (InputMismatchException | NumberFormatException exception) {
// This will catch errors due wrong input (ie user entered a letter
// instead of a digit) and execute the code within this block.
// Another possible Exception thrown is IllegalStateException.
System.err.println("There was an error. Details:");
exception.printStackTrace(System.err); // remove if you don't want to see the error trace
} finally { // this will get executed whether your code had errors or not
sc.close(); // dont forget to close your scanner
}
}
如果您的 IDE 未向您顯示有關方法的資訊,則可能沒有記錄該方法,或者您沒有匯入該檔案。對于這種情況,只需谷歌一下,您就會發現例外情況。例如,此鏈接顯示有關 Scanner.nextInt()和有關 Integer.parseInt()的資訊。
uj5u.com熱心網友回復:
代碼中的損壞部分被零除。處理例外
try {
int average = sum / array.length; // throw Exception
}
catch (ArithmeticException e) {
System.out.println(
"Divided by zero operation cannot possible");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/464777.html
標籤:爪哇
