Java 总分和百分比计算
我们将演示如何使用Java程序计算总分和百分比。术语 总分 指的是所有可用分数的总和,而术语 百分比 是通过将计算出的分数除以总分并将结果乘以100得到的数值。
percentage_of_marks = (obtained_marks/total_marks) × 100
示例1
这是一个Java程序,演示了如何计算总分和百分比。
// Java Program to demonstrate how is Total marks and Percentages calculated
import java.io.*;
public class TotalMarks_Percent1 {
public static void main(String[] args){
int n = 8, t_marks = 0;
float percent;
// creation of 1-D array to store marks
int marks[] = { 69, 88, 77, 89, 98, 100, 57, 78 };
// calculation of total marks
for (int j = 0; j < n; j++) {
t_marks += marks[j];
}
System.out.println("Total Marks is: " + t_marks);
// calculate the percentage
percent = (t_marks / (float)n);
System.out.println("Total Percentage is: " + percent + "%");
}
}
输出
Total Marks is: 656
Total Percentage is: 82.0%
在上面的Java程序中,计算了一个学生在8个科目中获得的总分和百分比。获得的分数存储在一个名为 marks[] 的数组中。
总分被计算并存储在一个名为 t_marks 的变量中,百分比被计算并存储在一个名为 percent 的变量中。
这两个值都进一步显示在控制台上。
示例2
这是一个Java程序,演示了从用户输入的五个科目的总分和百分比的计算。
// Java program to compute the total marks and percentage of five subjects taken as input from the user
import java.util.Scanner;
class TotalMarks_Percent2{
public static void main(String args[]){
float FLAT, COA, Networking, Python, AI;
double t_marks, percent;
Scanner mk =new Scanner(System.in);
// Take marks as input of 5 subjects from the user
System.out.println("Input the marks of five subjects \n");
System.out.print("Enter marks of FLAT:");
FLAT = mk.nextFloat();
System.out.print("Enter marks of COA:");
COA = mk.nextFloat();
System.out.print("Enter marks of Networking:");
Networking = mk.nextFloat();
System.out.print("Enter marks of Python:");
Python = mk.nextFloat();
System.out.print("Enter marks of AI:");
AI = mk.nextFloat();
// Calculation of total marks and percentage obtained in 5 subjects
t_marks = FLAT + COA + Networking + Python + AI;
percent = (t_marks / 500.0) * 100;
// display the results
System.out.println("Total marks obtained in 5 different subjects ="+t_marks);
System.out.println("Percentage obtained in these 5 subjects = "+percent);
}
}
输出
Input the marks of five subjects
Enter marks of FLAT:98
Enter marks of COA:56
Enter marks of Networking:67
Enter marks of Python:89
Enter marks of AI:78
Total marks obtained in 5 different subjects =388.0
Percentage obtained in these 5 subjects = 77.60000000000001
在上述的Java程序中,从用户输入了5门不同科目的成绩,分别是 FLAT 、 COA 、 Networking 、 Python 和 AI。
这些成绩被从用户输入并存储在float数据类型的变量中。进一步地,这些科目的总成绩通过各科目成绩的相加计算并存储在名为 t_marks 的变量中。
最后,程序显示所得科目的总成绩和百分比。
这篇文章阐明了两种计算总成绩和百分比的方法。文章首先讨论了术语百分比和总成绩。第一种方法讨论了没有从用户那里获取输入的方法,而在第二种方法中,将成绩的值从用户那里获取并计算它们的和与百分比,并将其显示出来。