C#循环问题,请帮我解决这个问题 [英] C# Loop problem please help me with this

查看:51
本文介绍了C#循环问题,请帮我解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;

namespace PrintPrice
{
    class Price
    {
       

        static void Main()
        {
            int lineNumber;
            int maximumLineNumber = 10;
            int totalPrice;
            int price = 10;
            int discout = 15;
            int discountPerLine3;
            int pricePerLine;
            for (lineNumber = 1; lineNumber <= maximumLineNumber; lineNumber++)
            {
                
                discountPerLine3 = lineNumber / 3;
                pricePerLine = lineNumber - (discountPerLine3 * 3);
                totalPrice = (discountPerLine3 * discout) + (pricePerLine * price);
                Console.WriteLine("Line " + lineNumber + ": " + totalPrice);

            }
            Console.ReadKey();
        }
    }
}


程序的输出是这样的


the out put of the program is like this

Line 1: 10
Line 2: 20
Line 3: 15
Line 4: 25
Line 5: 35
Line 6: 30
Line 7: 40
Line 8: 50
Line 9: 45
Line 10:55


我只想问一下如何制作这样的程序:


i just want to ask how to make this program like this:

Line 1: 10
Line 2: 15
Line 3: 15
Line 4: 25
Line 5: 30
Line 6: 30
Line 7: 40
Line 8: 45
Line 9: 45
Line 10:55

我应该使用什么语句?
还是在公式中?
请帮我.感谢

what statement should i use?
or is it in the formula?
please help me. thanks

推荐答案

循环没有问题.检查你的数学.拔出计算器并逐步操作.
There is nothing wrong with your loop. Check your math. Pull out the calculator and step through it.


以下是解决您问题的方法.我修改了for()循环以实现结果.这里的要点是需要计算的"totalPriceNext",以检查下一个结果是否小于上一个.

Following is the solution for your problem. I modified the for() loop to acheive the results. The main point here is the "totalPriceNext" needed to be computed to checked if this next result is less than the previous.

using System;
namespace PrintPrice
{
class Price
{
    static void Main()
    {
        int lineNumber;
        int maximumLineNumber = 10;
        int totalPrice;
        int price = 10;
        int discout = 15;
        //int discountPerLine3;
        //int pricePerLine;
        for (lineNumber = 1; lineNumber <= maximumLineNumber; lineNumber++)
        {
            totalPrice = Compute(lineNumber, price, discout);
            int totalPriceNext = Compute(lineNumber + 1, price, discout);
            if (totalPriceNext < totalPrice)
            {
                totalPrice = totalPriceNext;
            }
            Console.WriteLine("Line " + lineNumber + ": " + totalPrice);

        }
        //Console.ReadKey();
    }
    //returns totalprice
    public static int Compute(int plineNumber, int pprice, int pdiscout)
    {
        int discountPerLine3Local = (plineNumber / 3);
        int pricePerLineLocal = plineNumber - discountPerLine3Local * 3;
        int totalPriceLocal = (discountPerLine3Local * pdiscout) + (pricePerLineLocal * pprice);
        return totalPriceLocal;
    }
}
}


这篇关于C#循环问题,请帮我解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆