2016-10-09 49 views
-2

請幫助我..我一直在努力不懈地努力,我仍然無法按照我希望的方式運行。我對編程有所瞭解。我不擅長使用方法。我在這裏有我的工作樣本,我的問題是如何獲得平均分數,以及如何在控制檯上打印它。當我運行這段代碼時,變量級中的最後一個值是控制檯上顯示的唯一值。我還需要檢查出席情況......我該怎麼做? 我需要我的決賽代碼。如何計算數組中值的平均等級?

package studentmonitoring;

import java.util。*; 公共類StudentMonitoring {

public static void main(String[] args) { 
Scanner in=new Scanner(System.in); 

Date date=new Date(); 
    int[] attend1={},attend2,attend3,grade={}; 
String line="-------------------------------------------------------------------"; 
String[] idNum1={},idNum2={},gender={}, name1={},name2={},name3={},name4={},address={},sub={}; 

掃描儀輸入=新掃描儀(System.in); int check = 0,num = 0;

System.out.println(line); 
System.out.println("\t\t****STUDENT MONITORING****"); 
System.out.println(line); 

    System.out.println("\nGood day teacher, to start using this program input the following information first.\n"); 
    System.out.println(line); 
    System.out.print("NAME OF SCHOOL: "); 
    String sName=in.nextLine(); 
    System.out.print("ADDRESS OF SCHOOL: "); 
    String sAdd=in.nextLine(); 
    System.out.print("SCHOOL IDENTIFICATION NUMBER: "); 
    String sId=in.nextLine(); 
    System.out.println(line); 

    System.out.println("STUDENTS INFORMATION"); 
    System.out.println("Enter the year and section of this class"); 
    String yearSec=in.nextLine(); 
    System.out.println("How many student information do you want to input? "); 
    int numberStudents=in.nextInt(); 


    idNum1=new String[numberStudents]; 
    name1=new String[numberStudents]; 
    name2=new String[numberStudents]; 
    name3=new String[numberStudents]; 
    name4=new String[numberStudents]; 
    attend1=new int[200]; 
    attend2=new int[200]; 
    attend3=new int[200]; 
    address=new String[numberStudents]; 
    grade=new int[100]; 
    gender=new String[numberStudents]; 
     idNum2=new String[numberStudents]; 


    System.out.println("Enter the number of subjects you have: "); 
    int numSub=in.nextInt(); 

    sub=new String[numSub]; 
    in.nextLine(); 

    for(int x=0;x<numSub;x++) { 
    System.out.print("SubjeC#"+(x+1)+": "); 
    sub[x]=in.nextLine(); 
    } 
    for(int x=0;x<numberStudents;x++) { 
     in.nextLine(); 
     System.out.println(line); 
     System.out.println("Student #"+(x+1)); 
     System.out.print("LAST NAME: "); 
     name1[x]=in.nextLine(); 
     System.out.print("FIRST NAME: "); 
     name2[x]=in.nextLine(); 
     System.out.print("MIDDLE INITIAL: "); 
     name3[x]=in.nextLine(); 

     System.out.print("GENDER: "); 
     gender[x]=in.nextLine(); 

     System.out.print("ADDRESS: "); 
     address[x]=in.nextLine(); 

     System.out.print("ID NUMBER: "); 
     idNum1[x]=in.nextLine(); 


     System.out.println(line); 
     System.out.println("Enter the grades here."); 
     System.out.println(line); 
     for (int a=0;a<numSub;a++) { 
      System.out.print(sub[a]+": "); 
     grade[a]=in.nextInt(); 
     } 
     } 



    System.out.println("This is the list of the students info you've inputted."); 
     System.out.println("ID Number\t Name\t\t GENDER\t\t ADDRESS"); 


     for(int x=0;x<numberStudents;x++) { 

     System.out.println(idNum1[x]+"\t\t"+name1[x]+" "+name2[x]+","+name3[x]+".\t\t"+gender[x]+"\t\t"+address[x]); 
     } 

    System.out.println("Subject\t\tGrades"); 
    for(int x=0;x<numSub;x++) { 
     System.out.println(sub[x]+"\t\t"+grade[x]); 
    } 
    System.out.println(line); 

    System.out.println(); 
} 

}

+0

這個並行陣列設計不好。 – Enzokie

+0

請幫助我做出更好的一個。 – JohnHERE

+0

如果您已經開始使用OOP,則應該將其設計爲一個Student對象數組,而不是爲每個屬性創建一個單獨的數組。 – Enzokie

回答

1

下面是一個例子:

double[] values = { 2.2, 3.6, 6.3, 3.6, 2.8, 8.5, 8.3, 8.3, 1.2, 10, 2.2, 5.6 }; 
double sum = 0; 
for (int i = 0; i < values.length; i++) 
    sum += values[i]; 
System.out.println("Average: " + sum/values.length); 

,回答您最初的問題。

但是,如果你也有填充陣列的問題,你可以做到這一點:

int MAX = 3; 
double[] values = new double[MAX]; 
Scanner scanner = new Scanner(System.in); 
for (int i = 0; i < MAX; i++) { 
    System.out.print("Type the grade: "); 
    values[i] = scanner.nextDouble(); 
} 
scanner.close(); 
+0

我用單個數組變量來存儲不同學生的成績..這不是正確的做法嗎? – JohnHERE

+0

當然。你可以使用一個可變數組。沒有問題。我只是舉一個例子。但是您可以完美地聲明一個空數組,然後添加值。 –

+0

我還是不知道該怎麼辦......我非常渴望得到我的決賽的代碼......而且現在是6天... – JohnHERE

1
public static void main(String[] args) { 
    System.out.println("Enter the number of students you want: "); 
    Scanner scanner = new Scanner(System.in); 

    int numOfStudents = scanner.nextInt(); 

    double[] grades = new double[numOfStudents]; 

    double total = 0; 
    double currentGrade = 0; 

    for(int i=0; i<grades.length; i++) { 
     System.out.println("Grade achieved: "); 
     currentGrade = scanner.nextDouble(); 
     grades[i] = currentGrade; 
     total += currentGrade; 
    } 

    scanner.close(); 
    System.out.println(Arrays.toString(grades)); 
    System.out.println("Average grade " + (total/numOfStudents)); 
} 

在這裏,我們先設置雙數組的大小(這將填充等級)。然後我們使用for循環來填充雙數組,並通過scanner.nextDouble()獲取用戶輸入。

+0

我怎樣才能做到這一點使用數組變量? – JohnHERE

+0

我已更新您的評論的答案。 – Brunaldo

+0

我在我的代碼上試過這個,但問題是我必須計算多個學生的成績,當我運行我的程序時,它會因爲scanner.close()停止,當我拿出第一個學生的成績合計時到下一個學生的成績......我該怎麼辦? – JohnHERE