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