首頁  >  文章  >  Java  >  Java程式範例,用於計算總分和百分比

Java程式範例,用於計算總分和百分比

WBOY
WBOY轉載
2023-09-11 18:01:021071瀏覽

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的變數中。

這兩個值都會進一步顯示在控制台上。

Example 2

的中文翻譯為:

範例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個不同科目的成績,分別是FLATCOANetworkingPythonAI

標記作為使用者的輸入並儲存在浮點資料類型的變數中。此外,這些科目獲得的總分是透過將每個科目的分數相加來計算的,並儲存在名為t_marks的變數中。

最後,程式會顯示指定科目的總分及其百分比。

本文闡明了計算總分及其百分比的兩種方法。文章首先討論了學期百分比和總分。第一種方法討論的是不接受使用者輸入的方法,而第二種方法則將分數的值作為使用者的輸入,計算並顯示分數的總和和百分比。

以上是Java程式範例,用於計算總分和百分比的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除