У меня есть 3 Java программыRookie здесь, не знаю, где использовать блок попытаться поймать
первый один:
public class Person{
private String firstname;
private String lastname;
private int g1;
private int g2;
private int g3;
private int periodic;
public Person(String firstname,String lastname,int periodic,int g1,int g2,int g3){
this.firstname = firstname;
this.lastname = lastname;
this.periodic=periodic;
this.g1=g1;
this.g2=g2;
this.g3=g3;
}
public void print(){
int a=(g1+g2+g3)/3;
System.out.println("\t"+ firstname + " "+ lastname);
System.out.println("Periodic Exam: " + periodic);
System.out.println("Grade of quiz 1: "+g1);
System.out.println("Grade of quiz 2: "+g2);
System.out.println("Grade of quiz 3: "+g3);
System.out.println("Average of the quizzes: "+a);
}
}
Вторая:
public class Student extends Person{ //inherits Person//
public Student(String firstname,String lastname,int periodic,int g1,int g2,int g3){
super(firstname,lastname,periodic,g1,g2,g3);
}
public void print(){
System.out.println("Student Details:");
super.print();
}
}
третий:
public class Main{
public static void main(String[] args){
Person persons[] = new Person[3];
persons[0] = new Student("Robert","Almazan",91,90,85,86);
persons[1] = new Student("Dennis","Navan",92,89,84,87);
persons[2] = new Student("Allan","Villariza",93,79,81,84);
for(Person person:persons){
person.print();
}
}
Все, что я хочу сделать, это поймать исключения, когда я изменить человек [], как этого
Person persons[] = new Person[3];
persons[0] = new Student("Robert","Almazan","ZZ",0,-85,86);
persons[1] = new Student("Dennis","Navan","ASA",0,-84,87);
persons[2] = new Student("Allan","Villariza","sa",0,-81,84);
Я пытался делать это, как этого
public class Main{
public static void main(String[] args){
try{
Person persons[] = new Person[3];
persons[0] = new Student("Robert","Almazan","ZZ",90,85,86);
persons[1] = new Student("Dennis","Navan","ASA",89,84,87);
persons[2] = new Student("Allan","Villariza","9",79,81,84);
for(Person person:persons){
person.print();
}
}//try
catch(ArithmeticException e){
System.out.println("Arithmetic Error");
}
catch(InputMismatchException e){
System.out.println("Input Error");
}
finally{
System.out.println("Thank you");
}
}//main
}
исключение уловов программы Doesnt
Когда я компиляция, он по-прежнему говорит, что ошибки, как строка не могут быть преобразованный в int, он всегда принимает отрицательные числа и не исключает исключения
необходимо помощь ...
Моя Ява ржавая, но не должна «Личные лица [] = новое лицо [3];' be 'Person [] persons = new Person [3];'? – childofsoong
@ бессознательно, оба в порядке. Zer0: Вероятно, вы должны попытаться отделить отдельные проблемы: кто это? Компилятор? Какое исключение вы получаете? Почему вы ожидаете, что отрицательные числа не будут приняты? – junix