编写一个 Java 程序,查找五门科目或 N 门科目的总分、平均分和百分比,并附带示例。
Java 编程查找五门科目总分、平均分和百分比 示例 1
此 Java 程序允许用户输入五门科目的五个不同值。然后,此 Java 程序将查找这五门科目的总分、平均分和百分比。在此示例中,我们使用算术运算符来执行算术运算。
// Java program to find Total Average and percentage of Five Subjects
import java.util.Scanner;
public class Totalof5subjects1 {
private static Scanner sc;
public static void main(String[] args)
{
int english, chemistry, computers, physics, maths;
float total, Percentage, Average;
sc = new Scanner(System.in);
System.out.print(" Please Enter the Five Subjects Marks : ");
english = sc.nextInt();
chemistry = sc.nextInt();
computers = sc.nextInt();
physics = sc.nextInt();
maths = sc.nextInt();
total = english + chemistry + computers + physics + maths;
Average = total / 5;
Percentage = (total / 500) * 100;
System.out.println(" Total Marks = " + total);
System.out.println(" Average Marks = " + Average);
System.out.println(" Marks Percentage = " + Percentage);
}
}

Java 编程计算五门科目总分、平均分和百分比 示例 2
上面的 Java 程序是针对固定数量的科目的。在此 Java 程序中,用户可以选择任意数量的科目。然后,它会查找总分、平均分和百分比。
// Java program to find Total Average and percentage of Five Subjects
import java.util.Scanner;
public class Totalof5subjects2 {
private static Scanner sc;
public static void main(String[] args)
{
int totalSubjects, i;
float Marks, total = 0, Percentage, Average;
sc = new Scanner(System.in);
System.out.print(" Please Enter the Total Number of Subjects : ");
totalSubjects = sc.nextInt();
System.out.print(" Please Enter the Subjects Marks : ");
for(i = 0; i < totalSubjects; i++)
{
Marks = sc.nextInt();
total = total + Marks;
}
Average = total / totalSubjects;
Percentage = (total / (totalSubjects * 100)) * 100;
System.out.println(" Total Marks = " + total);
System.out.println(" Average Marks = " + Average);
System.out.println(" Marks Percentage = " + Percentage);
}
}
分析
- 第一个 Java System.out.print 语句要求用户输入科目总数。例如,3 门科目。
- For 循环通过使用条件 i < totalsubjects 来阻止用户输入超过 3 个值。在下一行,我们将用户输入的值添加到 Total。
- 循环外部,我们计算了平均值和百分比。
Please Enter the Total Number of Subjects : 6
Please Enter the Subjects Marks : 80 85 75 65 90 95
Total Marks = 490.0
Average Marks = 81.666664
Marks Percentage = 81.666664
Java 编程计算五门科目总分、平均分和百分比 示例 3
此程序与第一个示例相同。但在该 Java 程序中,我们创建了一个单独的方法来查找学生的总分、平均分和百分比。
// Java program to find Total Average and percentage of Five Subjects
import java.util.Scanner;
public class Totalof5subjects3 {
private static Scanner sc;
public static void main(String[] args)
{
int english, chemistry, computers, physics, maths;
sc = new Scanner(System.in);
System.out.print(" Please Enter the Five Subjects Marks : ");
english = sc.nextInt();
chemistry = sc.nextInt();
computers = sc.nextInt();
physics = sc.nextInt();
maths = sc.nextInt();
totalmarks(english, chemistry, computers, physics, maths);
}
public static void totalmarks(int eng, int chem, int com, int phy, int math)
{
float total, Percentage, Average;
total = eng + chem + com + phy + math;
Average = total / 5;
Percentage = (total / 500) * 100;
System.out.println(" Total Marks = " + total);
System.out.println(" Average Marks = " + Average);
System.out.println(" Marks Percentage = " + Percentage);
}
}
Please Enter the Five Subjects Marks : 65 75 85 95 90
Total Marks = 410.0
Average Marks = 82.0
Marks Percentage = 82.0