用于打印副本的 C# 程序

标签 c#

我的程序为复印的前 100 篇论文每打印一份收取 2 美元。如果用户打印出超过 hundread 份,每超过 hundread 每份需要 1 美元(所以如果我想打印 101 份,价格应该是 200 + 1,第 101 份 1 美元前 100 份每份 2 美元)。这是我的代码:

int CopyCost = 2;
        int ammountOfCopies;
        int discount = 1;
        Console.WriteLine("How many copies would you like?: ");
        ammountOfCopies = int.Parse(Console.ReadLine());


        for (int i = 0; i < ammountOfCopies; i++)
        {
        if (ammountOfCopies > 100)
            CopyCost = 2 - discount;
        else
            CopyCost =  2;

        CopyCost *= ammountOfCopies;
        }
        Console.WriteLine("The total cost for your copies is: {0} ", CopyCost);

        Console.ReadLine();

但我遇到的问题是,如果我选择写出 101 份,它会将每份折扣为 1 美元,而不仅仅是超过 100 份的。

最佳答案

这与其说是编码问题,不如说是数学问题。您需要将等于或低于 100 份的份数乘以正常价格。然后将超过 100 份的份数乘以折扣价。不需要 for 循环。

在代码中将问题分解成小块,例如,像这样:

int price = 2;
int discountedPrice = price - 1;
int amountAtNormalPrice = Math.Min(amountOfCopies, 100);
int amountAtDiscountPrice = Math.Max(amountOfCopies - 100, 0);
int amountTotal = (amountAtNormalPrice * price) + (amountAtDiscountedPrice * discountedPrice);

关于用于打印副本的 C# 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253867/

相关文章:

c# - Oracledependency 事件未触发

c# - 使用 NHibernate 进行慢速反射(额外调用 CodeAccessSecurityEngine)

c# - 除了 LINQ,有人发现 "var"的用途吗?

c# - 'System.Data.IDbConnection' 到 'System.Data.SqlClient.SqlConnection'

c# - C# 编译器不会在每次编译时立即报告所有错误吗?

C# 监听麦克风

c# - 如何从数组中删除元素

c# - 为什么我的对象被处置?

c# - 是否可以在 lambda 表达式中定位 EventHandler?

c# - 在 .Net 3.5 中使用 Ninject 时 System.Core 的 System.Io.FileNotFoundException