简体   繁体   中英

One Dimensional Java Array From User Input

coding newbie here. I am sure that my code is inefficient and could use a lot of brushing up.

Now to my question.

I need to take a users input for X number of items in an array, then print that array, grading each item.

The output should look something like, "Student [array position] scored [value of that array position]."

public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.print("Enter the number of test scores: ");
    int tests = input.nextInt();
    int [] scores = new int [tests];

    System.out.print("Enter " + tests + " scores: ");
    for (int i = 0; i < tests; i++) {
        scores[i] = input.nextInt();

        int best = scores[0];
        for (int j = 1; j < scores.length; j++) {
            if (scores[j] > best) best = scores[j];

        int gradeA = best - 10;
        int gradeB = best - 20;
        int gradeC = best - 30;
        int gradeD = best - 40;

        if (scores[i] >= gradeA){
            System.out.print("Student " + scores[j] +" score is "
                    + scores[i] + " and grade is A.");
        }
        else if (scores[i] <= gradeB){
            System.out.print("\nStudent " + scores[j] +" score is "
                    + scores[i] + " and grade is B.");
        }
        else if (scores[i] <= gradeC){
            System.out.print("\nStudent " + scores[j] +" score is "
                    + scores[i] + " and grade is C.");
        }
        else if (scores[i] <= gradeD){
            System.out.print("\nStudent " + scores[j] +" score is "
                    + scores[i] + " and grade is D.");
        }
        else{
            System.out.print("\nStudent " + scores[j] +" score is "
                    + scores[i] + " and grade is F.");
        }
        }
    }   
}

}

And what I get as output looks crazy:

Enter the number of test scores: 5 Enter 5 scores: 45 78 82 56 74 Student 0 score is 45 and grade is A.Student 0 score is 45 and grade is A.Student 0 score is 45 and grade is A.Student 0 score is 45 and grade is A.Student 78 score is 78 and grade is A.Student 0 score is 78 and grade is A.Student 0 score is 78 and grade is A.Student 0 score is 78 and grade is A.Student 78 score is 82 and grade is A.Student 82 score is 82 and grade is A.Student 0 score is 82 and grade is A.Student 0 score is 82 and grade is A. Student 78 score is 56 and grade is B. Student 82 score is 56 and grade is B. Student 56 score is 56 and grade is B. Student 0 score is 56 and grade is B.Student 78 score is 74 and grade is A.Student 82 score is 74 and grade is A.Student 56 score is 74 and grade is A.Student 74 score is 74 and grade is A.

Simply put, how do I fix my code to print 5 neat lines in the format ("Student [array position] scored [value of that array position].") stated above.

Fix 1 - Loops

Complete the loop

System.out.print("Enter " + tests + " scores: ");
for (int i = 0; i < tests; i++) {
    scores[i] = input.nextInt();
} // <------------- Finish loop here.

Complete it here too

for (int j = 1; j < scores.length; j++) {
    if (scores[j] > best) best = scores[j];
} // <------------- Finish loop here.

Start it again after calculating grades.

int gradeD = best - 40;

for (int i = 0; i < tests; i++) { // <------------- Started again.
    if (scores[i] >= gradeA){

Fix 2 - Index

Replace

System.out.print("Student " + scores[j] +" score is "
                + scores[i] + " and grade is A.");

with

System.out.println("Student " + i +" score is "
                + scores[i] + " and grade is A.");

Happy formatting.
Good luck.

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