C# 中的百分比计算
Muhammad Zeeshan
2022年4月20日
本文将演示如何在 C# 编程语言中计算百分比。
创建一个使用 C#
计算百分比的程序
首先,我们必须导入以下库。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
然后构造四个 double
类型变量,分别命名为 sub1
、sub2
、sub3
和 sub4
。还有一个名为 total
的变量将存储所有科目的总分。
另外,创建一个名为 per
的 double
类型百分比变量。
double sub1, sub2, sub3, sub4, total;
double per;
我们现在可以使用提供的公式计算百分比。
per = total / 4; //Here "total" holds the total marks of all subjects, and "4" is the total number of subjects.
完整源代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class PercentageCalculation
{
static void Main(string[] args)
{
double sub1, sub2, sub3,sub4, total;
double per;
Console.Write("Enter marks of subjects to find out the percentage. \n");
Console.Write("Input the marks of Subject1: ");
sub1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks of Subject2: ");
sub2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks of Subject3: ");
sub3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks of Subject4: ");
sub4 = Convert.ToInt32(Console.ReadLine());
total = sub1 + sub2 + sub3 + sub4;
per = total / 4;
Console.Write("Percentage = {0}%\n", per);
}
}
输出:
Enter marks of subjects to find out the percentage.
Input the marks of Subject1: 34
Input the marks of Subject2: 45
Input the marks of Subject3: 56
Input the marks of Subject4: 45
Percentage = 45%
Author: Muhammad Zeeshan
I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.
LinkedIn