我们将演示如何使用 java 程序计算总分和百分比。 总分是指所有可用分数的总和,而术语百分比是指计算分数除以总分并乘以所得的数字100。
percentage_of_marks = (obtained_marks/total_marks) × 100
示例 1这是一个java程序,用于演示如何计算总分和百分比。
// java program to demonstrate how is total marks and percentages calculatedimport 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: 656total percentage is: 82.0%
在上面的java程序中,正在计算学生在8门科目中获得的总分和百分比。获得的分数存储在名为marks []的数组中。
总分数被计算并存储在一个名为t_marks的变量中,它们的百分比被计算并存储在一个名为percent的变量中。
这两个值都会进一步显示在控制台上。
example 2的中文翻译为:示例2这是一个java程序,用于演示从用户输入中计算五个科目的总分和百分比。
// java program to compute the total marks and percentage of five subjects taken as input from the userimport 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:98enter marks of coa:56enter marks of networking:67enter marks of python:89enter marks of ai:78total marks obtained in 5 different subjects =388.0percentage obtained in these 5 subjects = 77.60000000000001
在上面的java程序中,从用户那里输入了5个不同科目的成绩,分别是 flat,coa,networking,python 和 ai。
标记作为用户的输入并存储在浮点数据类型的变量中。此外,这些科目获得的总分是通过将每个科目的分数相加来计算的,并存储在名为t_marks的变量中。
最后,程序会显示指定科目的总分及其百分比。
本文阐明了计算总分及其百分比的两种方法。文章首先讨论了学期百分比和总分。第一种方法讨论的是不接受用户输入的方法,而第二种方法则将分数的值作为用户的输入,计算并显示分数的总和和百分比。
以上就是java程序示例,用于计算总分和百分比的详细内容。