Java程序:演示总分和百分比计算
我们将演示如何使用Java程序计算总分和百分比。“总分”是指所有可用分数的总和,“百分比”是指计算出的分数除以总分数,再乘以100的结果。
percentage_of_marks = (obtained_marks/total_marks) × 100
问题陈述
给定一个Java程序,计算学生在给定科目集合中的总分和百分比。
输入
marks = { 69, 88, 77, 89, 98, 100, 57, 78 }
输出
Total Marks is: 656 Total Percentage is: 82.0%
演示总分和百分比计算的方法(无需用户输入)
以下是计算总分和百分比(无需用户输入)的步骤:
- 步骤1: 定义科目数量。
- 步骤2: 创建一个数组来存储每个科目的分数。
- 步骤3: 将所有分数相加以计算总分。
- 步骤4: 将总分除以科目数量再乘以100来计算百分比。
- 步骤5: 打印并显示总分和百分比。
示例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的变量中。
这两个值随后在控制台上显示。
演示总分和百分比计算的方法(有用户输入)
以下是计算总分和百分比(有用户输入)的步骤:
- 步骤1: 从用户处获取输入分数。
- 步骤2: 通过将所有分数相加来计算总分。
- 步骤3: 通过将总分除以最高可能分数(此处为500)来计算总百分比。
- 步骤4: 将总分除以科目数量再乘以100来计算百分比。
- 步骤5: 打印并显示总分和百分比。
示例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)中获得的分数。
分数作为用户输入,并存储在浮点型变量中。此外,通过将每个科目的分数相加来计算在这些科目中获得的总分,并将结果存储在名为t_marks的变量中。
最后,程序显示在所述科目中获得的总分及其百分比。
本文阐述了两种计算总分及其百分比的方法。文章首先讨论了百分比和总分的含义。第一种方法讨论了无需用户输入的方法,第二种方法从用户处获取分数的值,并计算和显示其总和和百分比。
广告
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP