简体   繁体   中英

How to print in if statement within a for-loop (arrays)

I'm wondering how can I print an item in if statement within a for-loop. When I print out the array, everything is fine, except for the item in the if statement (which is gradeValue). It just prints the very last item and copies that item for the previous lines.

    for(int i = 0; i < numberofcourses; i++)
    {
        scanner.nextLine();
        System.out.println("Please enter your #"+(i+1)+" class name: ");
        ClassName[i] = scanner.nextLine();
        System.out.println("Please enter your #"+(i+1)+" class description: ");
        Description[i] = scanner.nextLine();
        System.out.println("Please enter your #"+(i+1)+" class units: ");
        Units [i] = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Please enter your #"+(i+1)+" class grade: ");
        grade[i] = scanner.nextLine();

        if (grade[i].equals ("A"))
            gradeValue= 4.00;
          else if (grade[i].equals("A-"))
            gradeValue= 3.67;
          else if (grade[i].equals("B+"))
            gradeValue = 3.33;
          else if (grade[i].equals("B"))
            gradeValue = 3.00;
          else if (grade[i].equals ("B-"))
            gradeValue = 2.67;
          else if (grade[i].equals("C+"))
            gradeValue = 2.33;
          else if (grade[i].equals("C"))
            gradeValue = 2.00;
          else if (grade[i].equals ("D+"))
          gradeValue = 1.33;
          else if (grade[i].equals ("D"))
            gradeValue = 1.00;
          else if (grade[i].equals ("F"))
            gradeValue = 0;
          else if (grade[i].equals ("FX"))
            gradeValue = 0;
          else
            System.out.println ("Invalid Grade");
            finalgrade = gradeValue * Units[i];
    }

Here is an example of the output. As you can see, the GradePoint stays at 3.33, which is B+, the very last item I input. There is no sign of A and B- that I inputted in earlier. :(

Please enter the term of your grade calculation (for example, Fall 2015): 
Fall 2019
Please enter the number of courses that you are enrolled in Fall 2019: 
3
Please enter your #1 class name: 
FIN301
Please enter your #1 class description: 
Personal Finance
Please enter your #1 class units: 
3
Please enter your #1 class grade: 
A

Please enter your #2 class name: 
BUS314
Please enter your #2 class description: 
Business Finance
Please enter your #2 class units: 
3
Please enter your #2 class grade: 
B-

Please enter your #3 class name: 
BUS313
Please enter your #3 class description: 
Economics Finance
Please enter your #3 class units: 
3
Please enter your #3 class grade: 
B+
Class Grades - Fall 2019 Term
Office Grades
             Class               | Description               |  Units               |  Grade               | Gradepoint
------------------------------------------------------------------------------------------------------------------
            FIN301               | Personal Finance               |      3               |      A               |   3.33
            BUS314               | Business Finance               |      3               |     B-               |   3.33
            BUS313               | Economics Finance               |      3               |     B+               |   3.33

Your main problem is that in each iteration you calculate finalgrade but you do not store it in an array, like all the other, so it gets overwritten in the next iteration.
So you end up with the last value of finalgrade .
Change finalgrade to an array and use it like this:

finalgrade[i] = gradeValue * Units[i];

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