请帮助我..我一直在努力工作,我仍然无法按照我想要的方式运行。我是编程的新手。我不擅长使用方法。我在这里有一份工作样本,我的问题是如何获得成绩的平均值以及如何在控制台上打印。当我运行此代码时,变量等级中的最后一个值是控制台上显示的唯一值。我还需要检查出勤率......我该怎么做? 我需要这个代码用于我的决赛。
包学生监控;
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 :(得分: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();
答案 1 :(得分: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循环填充double数组,并通过scanner.nextDouble()获取用户输入。