多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Java 程序:计算和显示学生成绩 > 原文: [https://beginnersbook.com/2017/09/java-program-to-calculate-and-display-student-grades/](https://beginnersbook.com/2017/09/java-program-to-calculate-and-display-student-grades/) 该程序根据用户在每个科目中输入的标记计算学生的成绩。程序根据此逻辑打印等级。 如果标记的平均值`>= 80`则打印等级`'A'` 如果平均值`< 80`且`>= 60`则打印等级`'B'` 如果平均值`< 60`和`>= 40`然后打印等级`'C'` 否则打印等级`'D'` 要理解本程序,您应该具备以下 [Java](https://beginnersbook.com/java-tutorial-for-beginners-with-examples/) 概念的知识: * [Java `For`循环](https://beginnersbook.com/2015/03/for-loop-in-java-with-example/) * [Java 中的数组](https://beginnersbook.com/2013/05/java-arrays/) * [Java 中的`if..else-if`](https://beginnersbook.com/2017/08/if-else-statement-in-java/) ## 示例:显示学生成绩的程序 ```java import java.util.Scanner; public class JavaExample { public static void main(String args[]) { /* This program assumes that the student has 6 subjects, * thats why I have created the array of size 6\. You can * change this as per the requirement. */ int marks[] = new int[6]; int i; float total=0, avg; Scanner scanner = new Scanner(System.in); for(i=0; i<6; i++) { System.out.print("Enter Marks of Subject"+(i+1)+":"); marks[i] = scanner.nextInt(); total = total + marks[i]; } scanner.close(); //Calculating average here avg = total/6; System.out.print("The student Grade is: "); if(avg>=80) { System.out.print("A"); } else if(avg>=60 && avg<80) { System.out.print("B"); } else if(avg>=40 && avg<60) { System.out.print("C"); } else { System.out.print("D"); } } } ``` 输出: ```java Enter Marks of Subject1:40 Enter Marks of Subject2:80 Enter Marks of Subject3:80 Enter Marks of Subject4:40 Enter Marks of Subject5:60 Enter Marks of Subject6:60 The student Grade is: B ```