Java数组代码以max和min为单位

时间:2015-01-09 23:36:13

标签: java arrays max average min

我的项目是找到学生的平均,最低和最高等级。成绩和他们的地位,但它不断向我显示输出错误。


以下是姓名和成绩:

生:

Alia Nahid Eiman Suad Lamia Salma Mai Wedad Haya Sanaa

测验:

10 20 50 70 80 50 30 90 60 40

期中:

30 80 100 40 80 70 70 80 50 30

期末考试:

40 80 70 100 90 60 70 50 40 80

这是Java代码:

package java_final_project;

import java.util.*;

public class Java_Final_Project {
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        // Declare inputs:
        String letter1, letter2, letter3;
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;

        // Declare the four array structures
        int[] quiz = new int[10];
        int[] midterm = new int[10];
        int[] finalexam = new int[10];
        String[] students = new String[10];  

        // Input elements to the arrays
        int counter, sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;
        double ave1, ave2, ave3;
        int max1, max2, max3;
        int min1, min2, min3;

        for (counter = 0; counter < 10; counter++) {
            System.out.println ("Enter student names and grades of quiz, midterm "
                + "and final with space between them");
            students[counter] = console.next();
            quiz[counter] = console.nextInt();
            sum1 = sum1 + quiz[counter];
            midterm[counter]= console.nextInt();
            sum2 = sum2 + midterm[counter];
            finalexam[counter]= console.nextInt();
            sum3 = sum3 + finalexam[counter];
        }

        ave1 = sum1 / 10;
        ave2 = sum2 / 10;
        ave3 = sum3 / 10;

        // min and max values
        max1 = Math.max(quiz[counter], max);
        max2 = Math.max(midterm[counter], max);
        max3 = Math.max(finalexam[counter], max);

        min1 = Math.min(quiz[counter], min);
        min2 = Math.min(midterm[counter], min);
        min3 = Math.min(finalexam[counter], min);

        // if statement for the standing
        // ave1
        if (ave1 >= 90 && ave1 <= 100) {
            letter1 = "E";
        } else if (ave1 >= 70 && ave1 <= 80) {
            letter1 = "G";
        } else if (ave1 >= 50 && ave1 <= 60) {
            letter1 = "S";
        } else {
            letter1 = "P";
        }

        // ave2
        if (ave2 >= 90 && ave2 <= 100) {
            letter2 = "E";
        } else if (ave2 >= 70 && ave2 <= 80) {
            letter2 = "G";
        } else if (ave2 >= 50 && ave2 <= 60) {
            letter2 = "S";
        } else {
            letter2 = "P";
        }

        //ave3
        if (ave3 >= 90 && ave3 <= 100) {
            letter3 = "E";
        } else if (ave3 >= 70 && ave3 <= 80) {
            letter3 = "G";
        } else if (ave3 >= 50 && ave3 <= 60) {
            letter3 = "S";
        } else {
            letter3 = "P";
        }

        // Display the elements of the four arrays  
        System.out.println("Here is the elements of the four arrays");
        System.out.println("students" + "\t" + "quiz" + "\t" + "midterm" + "\t" + "finalexam");
        for (counter = 0; counter < 10; counter++) {
            System.out.println(students[counter] + "\t" + quiz[counter] + "\t" + midterm[counter] + "\t" + finalexam[counter]);
        }
        System.out.println("Summary Report:");
        System.out.println();
        System.out.println("students" + "\t" + "\t" + "Quiz" + "\t" + "\t" + "Midterm" + "\t" + "\t" + "FinalExam");
        System.out.println();
        System.out.println("Average" + "\t" + "\t" + ave1 + "\t" + "\t" + ave2 + "\t" + "\t" + ave3);
        System.out.println();
        System.out.println("Max" + "\t" + "\t" + max1 + "\t" + "\t" + max2 + "\t" + "\t" + max3);
        System.out.println();
        System.out.println("Min" + "\t" + "\t" + min1 + "\t" + "\t" + min2 + "\t" + "\t" + min3);
        System.out.println();
        System.out.println("Standing" + "\t" + "\t" + letter1 + "\t" + "\t" + letter2 + "\t" + "\t" + letter3);
    }
}

3 个答案:

答案 0 :(得分:0)

您设置min1, min2, min3, max1, max2, max3的六行需要在您收集输入的for循环内;因为你想在每次输入后检查它是否大于或小于当前的最大值和最小值。如果您目前拥有这些行,那么它们就无法工作,因为counter不再指向数组中的条目。

答案 1 :(得分:0)

我不确定您是否被允许在作业中使用流。如果你有一个更简单的方法来获得Java 8中的min,max,average:

int[] scores = {1, 3, 6, 8, 10, 11, 2};
int max = Arrays.stream(scores).max();
int min = Arrays.stream(scores).min();
int avg = Arrays.stream(scores).average();

由于Java有流,你现在不需要经常使用循环。

答案 2 :(得分:0)

我已经找出了代码并纠正了一些错误。这是更新的java代码,它有效:D!我感谢您的意见和帮助:))

package java_final_project;



import java.util.*;

public class Java_Final_Project



{

static Scanner console = new Scanner(System.in);

public static void main(String[] args)



{
    // Declare inputs:
    String letter1, letter2, letter3;
    double ave1, ave2, ave3;

    // Declare the four array structures
    int[] quiz = new int[10];
    int[] midterm = new int[10];
    int[] finalexam = new int[10];
    String [] students = new String[10]; 
    int [] scores = new int[100]; 

    // Input elements to the arrays
    int counter, sum1=0;
    int sum2=0;
    int sum3=0;

    for ( counter = 0; counter < 10; counter++)
    {
    System.out.println ("Enter student names and grades of quiz, midterm "
    + "and final with space between them");

    students[counter] = console.next();
    quiz[counter]= console.nextInt();
    sum1 = sum1 + quiz[counter];
    midterm[counter]= console.nextInt();
    sum2 = sum2 + midterm[counter];
    finalexam[counter]= console.nextInt();
    sum3 = sum3 + finalexam[counter];                     
    }

    // max and min values for the quiz
    int minGrade = quiz[0];
    int maxGrade = quiz[0];
    for (int i = 0; i < 10; i++)
{ 
if (minGrade > quiz[i])
  minGrade = quiz[i];
if (maxGrade < quiz[i])
  maxGrade = quiz[i];
}

// max and min values for the midterm
int minGrade1 = midterm[0];
int maxGrade1 = midterm[0];
    for (int i = 0; i < 10; i++)
    { if (minGrade1 > midterm[i])
  minGrade1 = midterm[i];
if (maxGrade1 < midterm[i])
  maxGrade1 = midterm[i];}

// max and min values for the final
int minGrade2 = finalexam[0];
int maxGrade2 = finalexam[0];
for (int i = 0; i < 10; i++)
{if (minGrade2 > finalexam[i])
  minGrade2 = finalexam[i];
if (maxGrade2 < finalexam[i])
  maxGrade2 = finalexam[i];}

    //calculate the average of quiz, midterm, final        
    ave1 = sum1/10;
    ave2 = sum2/10;
    ave3 = sum3/10;

    // if statement for the standing
    // ave1
        if (ave1 > 90 && ave1 > 100)
        {letter1 = "E";}
       else if (ave1 > 70 && ave1 > 80)
        {letter1 = "G" ;}
       else if (ave1 > 50 && ave1 > 60)
        {letter1 = "S";}
       else 
       {letter1 = "P";}

    // ave2
       if (ave2 > 90 && ave2 > 100)
        {letter2 = "E";}
       else if (ave2 > 70 && ave2 > 80)
        {letter2 = "G" ;}
       else if (ave2 > 50 && ave2 > 60)
        {letter2 = "S";}
       else 
       {letter2 = "P";}

    //ave3
       if (ave3 > 90 && ave3 > 100)
        {letter3 = "E";}
       else if (ave3 > 70 && ave3 > 80)
        {letter3 = "G" ;}
       else if (ave3 > 50 && ave3 > 60)
        {letter3 = "S";}
       else 
       {letter3 = "P";}

   // Display the elements of the four arrays  
    System.out.println (" Here is the elements of the four arrays");
    System.out.println ("students"+"\t"+"quiz"+"\t"+"\t"+"midterm"+"\t"+"\t"+"finalexam");
    for ( counter = 0; counter < 10; counter++)
    {

    System.out.println (students[counter]+"\t"+"\t"+quiz[counter]+"\t"+"\t"+midterm[counter]
            +"\t"+"\t"+finalexam[counter]);

    }

     System.out.println ("  ");
     System.out.println ("Summary Report:");
     System.out.println ("\t"+"\t"+"Quiz"+"\t"+"\t"+"Midterm"+"\t"+"\t"+"FinalExam");
     System.out.println ("Average"+"\t"+"\t"+ave1+"\t"+"\t"+ave2+"\t"+"\t"+ave3);
     System.out.println ("max"+ "\t"+"\t"+maxGrade+ "\t"+"\t"+ maxGrade1+ "\t"+"\t"+ maxGrade2);
     System.out.println ("min"+ "\t"+"\t"+minGrade+ "\t"+"\t"+ minGrade1+ "\t"+"\t"+ minGrade2);
     System.out.println ();
     System.out.println ("Standing"+ "\t"+ letter1+ "\t"+ "\t"+ letter2+ "\t"+"\t"+ letter3);
}    
}