简体   繁体   English

如何在for循环(数组)中的if语句中打印

[英]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. 我想知道如何在for循环中的if语句中打印项目。 When I print out the array, everything is fine, except for the item in the if statement (which is gradeValue). 当我打印出数组时,一切都很好,除了if语句中的项目(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. 正如您所看到的,GradePoint保持在3.33,即B +,这是我输入的最后一项。 There is no sign of A and B- that I inputted in earlier. 我之前输入的A和B-没有任何迹象。 :( :(

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. 您的主要问题是,在每次迭代中,您都会计算finalgrade但不会将其存储在数组中,就像所有其他迭代一样,因此它会在下一次迭代中被覆盖。
So you end up with the last value of finalgrade . 所以你最终得到了finalgrade的最后一个值。
Change finalgrade to an array and use it like this: finalgrade更改为数组并使用它如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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