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個不同科目的成績,分別是FLAT,COA,Networking,Python 和AI。
標記作為使用者的輸入並儲存在浮點資料類型的變數中。此外,這些科目獲得的總分是透過將每個科目的分數相加來計算的,並儲存在名為t_marks的變數中。
最後,程式會顯示指定科目的總分及其百分比。
本文闡明了計算總分及其百分比的兩種方法。文章首先討論了學期百分比和總分。第一種方法討論的是不接受使用者輸入的方法,而第二種方法則將分數的值作為使用者的輸入,計算並顯示分數的總和和百分比。
以上是Java程式範例,用於計算總分和百分比的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

Java是熱門程式語言,適合初學者和經驗豐富的開發者學習。本教學從基礎概念出發,逐步深入解說進階主題。安裝Java開發工具包後,可透過建立簡單的「Hello,World!」程式來實踐程式設計。理解程式碼後,使用命令提示字元編譯並執行程序,控制台上將輸出「Hello,World!」。學習Java開啟了程式設計之旅,隨著掌握程度加深,可創建更複雜的應用程式。
