0
即時通訊試圖自學java,下面的代碼是我的嘗試。我沒有得到預期的輸出。我已經在輸出中發佈了我的編碼。爲什麼我沒有獲得穩定的產出?
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
要使用很多代碼。更準確地確定問題並回來 – Dici
定義「不符合預期」。 – Pshemo
檢查您調用的函數的順序's.getdata(); ---- s.show(); -----> s.calculate();'.total未在'getData() ''你在'show()'中打印'total'給你默認的int'0'。在第二次嘗試中它的工作原因是'total'初始化了第一次嘗試n'calculate()' – silentprogrammer