用於計算百分比的 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

更新日期:2024 年 6 月 14 日

33K+ 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.