用于计算百分比的 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