我正在尝试输入学生并输入他们的课程作业和考试结果以及我遇到的问题是找到平均总分,最低总分并按总分数打印所有学生最高 - 最低
import java.util.*;
import java.text.*;
public class Results
{
static String[] name = new String[100];
static int[] coursework = new int[100];
static int[] exam = new int[100];
static int[] totalScore = new int[100];
static String[] totalGrade = new String[100];
static String[]examGrade = new String[100];
static int count = 0;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
boolean flag = true;
while(flag)
{
System.out.println(
"1. Add Student\n" +
"2. List All Students\n" +
"3. List Student Grades\n" +
"4. Total Score Average\n" +
"5. Highest Total Score\n" +
"6. Lowest Total Score\n" +
"7. List all Students and Total Scores\n" +
"8. Quit\n");
System.out.print("Enter choice (1 - 8): ");
int choice = input.nextInt();
switch(choice)
{
case 1:
add();
break;
case 2:
listAll();
break;
case 3:
listGrades();
break;
case 4:
average();
break;
case 5:
highestTotal();
break;
case 6:
lowestTotal();
break;
case 7:
order();
break;
case 8:
flag = false;
break;
default:
System.out.println("\nNot an option\n");
}
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
}
System.out.println("\n\nHave a nice day");
}//end of main
static void add()
{
Scanner input = new Scanner(System.in);
System.out.println("Insert Name: ");
String names = input.nextLine();
System.out.println("Insert Coursework: ");
int courseworks = input.nextInt();
System.out.println("Insert Exam: ");
int exams = input.nextInt();
int totalscores = exams + courseworks;
name[count] = names;
coursework[count] = courseworks;
exam[count] = exams;
totalScore[count] = totalscores;
count++;
}
static void listAll()
{
for(int i=0;i<count;i++)
{
System.out.printf("%s %d %d\n", name[i], coursework[i], exam[i]);
}
}
static void listGrades()
{
for(int i=0;i<count;i++){
if(coursework[i] + exam[i] > 79)
{
System.out.println(name[i] + " HD");
}
else if(coursework[i] + exam[i] > 69)
{
System.out.println(name[i] + " DI");
}
else if(coursework[i] + exam[i] > 59)
{
System.out.println(name[i] + " CR");
}
else if(coursework[i] + exam[i] > 49)
{
System.out.println(name[i] + " PA");
}
else
{
System.out.println(name[i] + " NN");
}
}
}
static void average()
{
double sum = 0;
for(int i=0; i < count; i++)
{
sum += exam[i] + coursework[i];
}
sum = sum / count;
System.out.printf("Average Total Score : %.1f\n ", sum);
}
static void highestTotal()
{
int largest = totalScore[0];
String student = name[0];
for (int i = 0; i < exam.length; i++) {
if (totalScore[i] > largest) {
largest = totalScore[i];
student = name[i];
}
}
System.out.printf(student + ": " + largest + "\n");
}
static void lowestTotal()
{
int lowest = totalScore[0];
String student = name[0];
for (int i = 0; i > exam.length; i++) {
if (totalScore[i] < lowest) {
lowest = totalScore[i];
student = name[i];
}
}
System.out.printf(student + ": " + lowest + "\n");
}
static void order()
{
for (int i=0;i<count;i++)
{
Arrays.sort(totalScore);
System.out.printf(name[i] + "\t" + totalScore[count] + "\n");
}
}
}
答案 0 :(得分:2)
static void average(){
int total = 0;
for(int i = 0; i < array.length; i++)
{
total += array[i];
}
int average = total / array.length
}
以上代码可以获得平均值。你做了类似的事情来找到最大的价值。 排序数组只需使用它将解决您的问题。
Arrays.sort(array);
答案 1 :(得分:0)
对于排序,您可以使用Arrays.sort(数组)(如果需要特殊排序,请使用比较器) 一旦排序,获得最低分数应该很容易。要获得平均值,请将每个值相加并除以元素数。要全部添加它们,请使用for循环或for-each循环:
int sum = 0;
for(int i = 0; i < array.length; i++)
{
sum += array[i];
}
int average = sum / array.length
您可能希望使用double而不是int来表示总和。
答案 2 :(得分:0)
对于平均总分,只需添加课程作业和考试分数,例如总和除以否。学生:
static void average() {
int sum = 0;
for(int i=0; i < count; i++)
{
sum += exam[i] + coursework[i];
}
sum = sum / count;
System.out.printf("Average Total Score = : " + sum + "\n");
}
对于最低总分和最高总分,您实施的最高总分几乎是正确的。问题是您没有考虑最大变量中的 coursework [i] 。在类似的行上,您可以实现最低总分
static void highestTotal() {
int largest = exam[0] + coursework[0];
String student = name[0];
for (int i = 0; i < exam.length; i++) {
if (exam[i]+coursework[i] > largest) {
largest = exam[i] + coursework[i];
student = name[i];
}
}
System.out.printf(student + ": " + largest + "\n");
}
为了按顺序打印乐谱,我建议您定义另一个具有考试成绩和课程作业总分的int数组,使用任何基本排序技术对其进行排序并打印数组。请记住,在排序过程中交换总和时,还要交换相应的名称 ..