简体   繁体   中英

Storing array with loop and compare highest and lowest value

the program successfully compile and run. however, the value for the minimum score is wrong, i check again and cant figure out why pls help me solve this

/* import neccessary component for the program*/
import java.util.*;

public class assign4 {

    /* create a input stream*/ 
    static Scanner console = new Scanner(System.in);

    public static void main(String[]args) {

        /* declare variables and strings*/
        int[] score;

        score = new int[10];
        String[] name;

        name = new String[10];
        String header = String.format("%10s%10s%8s%n", "Name", "Score", "Grade");
        int min = score[0];
        int max = score[0];
        char grade[];

        grade = new char[10]; 
        int maxindex = 0;
        int lowindex = 0;

        for (int i = 0; i <= 9; i++) {

            /* Get user's input for student's name and score*/   
            System.out.println("Please enter the student's name.");
            name[i] = console.nextLine();
            System.out.println("Please enter the student's score.( 0-100 )");
            score[i] = console.nextInt();

            if (score[i] > 80) {
                grade[i] = 'A';
            } else if (score[i] > 65) {
                grade[i] = 'B';
            } else if (score[i] > 40) {
                grade[i] = 'C';
            } else if (score[i] > 20) {
                grade[i] = 'D';
            } else {
                grade[i] = 'E';
            } 

            /* when the score is higher than the score, it become maximum score*/
            if (score[i] > max) {
                max = score[i];
                maxindex = i;
            } /* when the score is lower than the score, it become minimum score*/ else if (score[i]
                    < min) {
                min = score[i];
                lowindex = i;
            } /* when the score neither lower or higher than the score, it will be ignored and program         
             do nothing*/ else {} 

            /* avoid scanner skipping in order to capture user's input */
            console.nextLine();
        }

        /* print out the stored information of the students and show higest and lowest score */
        System.out.println();
        System.out.print(header);
        for (int i = 0; i <= 9; i++) {
            System.out.printf("%10s%10d%8s%n", name[i], score[i], grade[i]);
        } 
        System.out.printf("%s obtains lowest score of %d%n", name[lowindex], min);
        System.out.printf("%s obtains higest score of %d%n", name[maxindex], max); 

    }
} 

the program purpose is to collect 10 person score and name, then print out name, score, highest score, lowest score and grade. Also, i just join this website, if the method i pose and asking is wrong, do tell and im sorry

You initialize int min = score[0]; which will be 0, because score is an int[] with 0 as default value for every entry.

You probably don't get any scores below 0, so if(score[i]<min) will only be true for negative values. Try initializing min with Integer.MAX_VALUE .

Also it's good practice to initialize max with Integer.MIN_VALUE , but it shouldn't matter in your case.

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