用於計算百分比的 Java 程式
百分比表示百分比(百),即 100 中的一部分的比率。百分比的符號為 %。我們通常會計算所得分數、投資回報率等的百分比。百分比也可以超過 100%。
例如,假設我們有整體和部分。那麼我們會說,部分是整體的多少百分比,計算公式如下:
percentage = ( part / total ) × 100
演算法
以下是使用 Java 計算百分比的演算法:
1. Collect values for part and total
2. Apply formula { percentage = ( part / total ) × 100 }
3. Display percentage示例
import java.util.Scanner;
public class Percentage {
public static void main(String args[]){
float percentage;
float total_marks;
float scored;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your marks ::");
scored = sc.nextFloat();
System.out.println("Enter total marks ::");
total_marks = sc.nextFloat();
percentage = (float)((scored / total_marks) * 100);
System.out.println("Percentage ::"+ percentage);
}
}輸出
Enter your marks :: 500 Enter total marks :: 600 Percentage ::83.33333
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP