简体   繁体   中英

Java - print user input of 4 integers, and the lowest, highest and average number

This program is supposed to take user input of four grades, take these grades and calculate the lowest, highest and average of them. Then it needs to print out the four grades along with the lowest, highest, and average of them with proper labels. I cannot figure out how to print out the four grades with my code, and for some reason it prints out the lowest, highest and average after every iteration of the loop, or every user input.

Here is what I have so far:

public class Test2 {

double total = 0.0;
double max = 0.0;
double min = Double.MAX_VALUE;

public void Test2 (double[] grades){
//Loop through all of the grades.
for(int i = 0; i < 4; i++){

    double grade = grades[i];
    //Add the grade to the total
    total += grade;

    //If this is the highest grade we've encountered, set as the max.
    if(max < grade){
        max = grade;
    }
    //If this is the lowest grade we've encountered, set as min.
    if(min > grade){
        min = grade;
    }
}
    System.out.println("Average is: " + (total / 4));
    System.out.println("Max is: " + max);
    System.out.println("Min is: " + min); }

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

    System.out.println("Please enter number");

    for (int i = 0; i < grades.length; i++) {
        grades[i] = input.nextDouble();
        Test2 g = new Test2();
        g.Test2(grades);
    } } }

Can anyone help me with this? I need it to print out the four grades (user input), along withe the lowest, highest and average grade from the four grades, but ONLY ONCE, not after every iteration of the loop. Sorry if my code looks bad.

You have to call the method Test2(double grade) only once in the main method as there is a for loop inside Test2 method. Ie call Test2 method in main outside for loop.

Your answer should be the below class.

import java.util.Scanner;

public class Test2 {

double total = 0.0;
double max = 0.0;
double min = Double.MAX_VALUE;

public void doOperations(double[] grades) {
    for (int i = 0; i < 4; i++) {

        double grade = grades[i];
        //Add the grade to the total
        total += grade;

        //If this is the highest grade we've encountered, set as the max.
        if (max < grade) {
            max = grade;
        }
        //If this is the lowest grade we've encountered, set as min.
        if (min > grade) {
            min = grade;
        }
    }
    System.out.println("Average is: " + (total / 4));
    System.out.println("Max is: " + max);
    System.out.println("Min is: " + min);
}

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

        System.out.println("Please enter number");

        for (int i = 0; i < grades.length; i++) {
            grades[i] = input.nextDouble();
        }
        Test2 test2 = new Test2();
        test2.doOperations(grades);


}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM