import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
abstract class q2Student
{
String name;
int roll_no;
int sub1,sub2,sub3,sub4,sub5;
int total;
float grade;
float per;
public abstract void getdata() throws IOException;
//public abstract void show();
public void show()
{
System.out.println ("Roll No. = "+roll_no);
System.out.println ("Name = "+name);
System.out.println ("Marks of 1st Subject = "+sub1);
System.out.println ("Marks of 2nd Subject = "+sub2);
System.out.println ("Marks of 3rd Subject = "+sub3);
System.out.println ("Marks of 4th Subject = "+sub4);
System.out.println ("Marks of 5th Subject = "+sub5);
System.out.println ("Total Marks = "+total);
System.out.println ("Percentage = "+per+"%");
System.out.println("Grade="+grade);
}
}
class StudDetails extends q2Student{
public void getdata() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println ("Enter Name of Student");
name = br.readLine();
System.out.println ("Enter Roll No. of Student");
roll_no = Integer.parseInt(br.readLine());
System.out.println ("Enter marks out of 100 of 1st subject");
sub1 = Integer.parseInt(br.readLine());
while(sub1>100){
System.out.println ("you have entered invalid mark, please check again and enter a mark out of 100");
sub1 = Integer.parseInt(br.readLine());
}
System.out.println ("Enter marks out of 100 of 2nd subject");
sub2 = Integer.parseInt(br.readLine());
while(sub2>100){
System.out.println ("you have entered invalid mark, please check again and enter a mark out of 100");
sub2 = Integer.parseInt(br.readLine());}
System.out.println ("Enter marks out of 100 of 2nd subject");
sub3 = Integer.parseInt(br.readLine());
while(sub3>100){
System.out.println ("you have entered invalid mark, please check again and enter a mark out of 100");
sub3 = Integer.parseInt(br.readLine());}
System.out.println ("Enter marks out of 100 of 2nd subject");
sub4 = Integer.parseInt(br.readLine());
while(sub4>100){
System.out.println ("you have entered invalid mark, please check again and enter a mark out of 100");
sub4 = Integer.parseInt(br.readLine());}
System.out.println ("Enter marks out of 100 of 2nd subject");
sub5 = Integer.parseInt(br.readLine());
while(sub5>100){
System.out.println ("you have entered invalid mark, please check again and enter a mark out of 100");
sub5 = Integer.parseInt(br.readLine());
}
}
public void calculate(){
total=sub1+sub2+sub3+sub4+sub5;
per=(total*100)/500;
grade=per/10;
}
public void result(){
if(grade>=9){
System.out.println("Execellent!!passed with distinction");
}
else if((grade>=7.5) &&( grade<=8.9)){
System.out.println("first class!!passed with distinction");
}
else if((grade>=6)&&(grade<=7.4)){
System.out.println("passed!! first class");
}
else{
System.out.println("sorry!! you are failed");
}
}
}
public class Student extends StudDetails{
static int n;
public static void main(String args[]) throws IOException
{
Student s=new Student();
System.out.println ("enter the total no of students");
Scanner in=new Scanner(System.in);
n=in.nextInt();
System.out.println ("the total no of students"+n);
Student id[]=new Student[n];// array object
for(int i=0;i<id.length;i++){
s.getdata();
s.show();
s.calculate();
s.result();
}
//in.close();
}
}
我的输出如下,我将对象数组输入为2,计算出的输出不是我预期的
enter the total no of students
2
the total no of students2
Enter Name of Student
ram
Enter Roll No. of Student
1
Enter marks out of 100 of 1st subject
89
Enter marks out of 100 of 2nd subject
98
Enter marks out of 100 of 2nd subject
78
Enter marks out of 100 of 2nd subject
76
Enter marks out of 100 of 2nd subject
87
Roll No. = 1
Name = ram
Marks of 1st Subject = 89
Marks of 2nd Subject = 98
Marks of 3rd Subject = 78
Marks of 4th Subject = 76
Marks of 5th Subject = 87
Total Marks = 0 //total im getting 0
Percentage = 0.0%
Grade=0.0 // grade 0 but result states passed with first class
passed!! first class
Enter Name of Student
sur
Enter Roll No. of Student
2
Enter marks out of 100 of 1st subject
65
Enter marks out of 100 of 2nd subject
67
Enter marks out of 100 of 2nd subject
89
Enter marks out of 100 of 2nd subject
86
Enter marks out of 100 of 2nd subject
87
Roll No. = 2
Name = sur
Marks of 1st Subject = 65
Marks of 2nd Subject = 67
Marks of 3rd Subject = 89
Marks of 4th Subject = 86
Marks of 5th Subject = 87
Total Marks = 350 // same method called in second index calculating properly
Percentage = 70.0%
Grade=7.0
passed!! first class
答案 0 :(得分:1)
for(int i=0;i<id.length;i++){
s.getdata();
s.calculate();
s.show();
s.result();
}
你需要在展示之前计算。我不确定,但我没有仔细阅读你的所有代码。您应该会看到How do I ask a good question?和How to create a Minimal, Complete, and Verifiable example.
的帮助中心