有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何计算数组中值的平均等级?

请帮帮我。。我一直在不停地工作,但我仍然无法让它按我想要的方式运行。我是编程新手。我不太擅长使用方法。我这里有一个我的工作样本,我的问题是如何得到平均成绩,如何在控制台上打印出来。当我运行此代码时,变量grade中的最后一个值是控制台上显示的唯一值。我还需要检查出勤情况。。。我该怎么做? 我期末考试需要这个密码

学生监控

导入java。util.*; 公共班级学生监控{

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();
}

}


共 (1) 个答案

  1. # 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();