C# 硬币兑换只使用循环,C语言中没有数组#

C# 硬币兑换只使用循环,C语言中没有数组#,c#,loops,C#,Loops,因此,我最近开始学习编程,因为我在课堂上学习了一门课程。到目前为止,我们所学到的最复杂的事情是循环,我们知道我是否需要处理它 在本练习中,程序询问您购买的任何东西的价格和花费的金额。我们有价值50 20 10 5 2和1的无限硬币。如果你的花费超过了它的成本,该计划将给你改变 例如: 价格?:44。 付了钱?:100。 更改为56:50 5 1 我已经试着把它简化,这样我在理解它之后就可以跳进整个事情 我的逻辑是尝试制作一个类似的程序,但只使用一枚硬币 int change, payment,

因此,我最近开始学习编程,因为我在课堂上学习了一门课程。到目前为止,我们所学到的最复杂的事情是循环,我们知道我是否需要处理它

在本练习中,程序询问您购买的任何东西的价格和花费的金额。我们有价值50 20 10 5 2和1的无限硬币。如果你的花费超过了它的成本,该计划将给你改变

例如: 价格?:44。 付了钱?:100。 更改为56:50 5 1

我已经试着把它简化,这样我在理解它之后就可以跳进整个事情

我的逻辑是尝试制作一个类似的程序,但只使用一枚硬币

int change, payment, price;
if (payment == price)
            {
                Console.Write("\nNo change needed, you paid with the exact value.\n");
            }
            else if (payment < price)
            {
                Console.Write("\nNot enough money.\n");
            }
            else {
                Console.Write("Change is {0}: ", change);
                 
            
                for (loop100 = 0; loop100 < change; loop100+=100)
                {
                    if (change > 100)
                    {
                        Console.Write(100 + (" "));
                    }
                }
                }
现在,在纸上这就是我要找的。但是,当使用不同的值设置另一个for循环时,问题就来了。如果我复制我的for/If循环,但更改
loop100
for,让我们说一个
loop50
并将其设置为一个50值硬币,首先,它会进行一个50的无休止循环,其次,我不会得到100+50硬币的最有效组合,而是每种硬币的变化。我会得到100-5次和50-10次

我花了好几个小时来解决这个问题,但我解决不了。我在网上看了看,每个人似乎都在使用数组,但我的老师不希望我们使用数组,因为我们还没有了解数组,而且比这简单得多


我应该尝试什么样的for循环?我是不是让它变得比现在更难了?

而循环可能会更好。跟踪还有多少变化需要交付

    while( change >= 100)
    {
        change -= 100;
        Console.Write(100 + (" "));
    }
    while( change >= 50)
    {
        change -= 50;
        Console.Write(50 + (" "));
    }

我个人的偏好是使用
while
循环。在其中,您可以使用嵌入的
if
语句或
for
循环在一系列硬币值之间移动:

/// <summary>
/// Ouput the change from the paid amount for the specified price.
/// </summary>
/// <param name="price">An int indicating the price of the purchase.</param>
/// <param name="paid">An int indicating the total amount paid.</param>
private static void outputChange(int price, int paid)
{
    int change = paid - price;
    int[] coins = new[] { 50, 20, 10, 5, 2, 1 };

    if (change > 0)
    {
        Console.Write("Change: ");
        while (change > 0)
        {
            for (int i = 0; i < coins.Length; i++)
            {
                if (change >= coins[i])
                {
                    Console.Write($"{coins[i]} ");
                    change -= coins[i];
                    break;
                }
            }
        }
        Console.Write("\n");
    }
    else
        Console.WriteLine("No change needed, you paid with the exact value.");
}
输出:

Change: 50 5 1 
Change: 50 20 5 2 
Change: 10 2 2 
No change needed, you paid with the exact value.
编辑以下评论

OP添加了一条评论,表示他们不希望使用阵列来制作硬币:


是否可以通过为创建值来使用相同的while+for语句 每一种硬币?我现在真的必须避免使用数组

在这种情况下,另一种(不太优雅的)方法可以使用
if
语句来计算下一枚硬币,见下文:

/// <summary>
/// Ouput the change from the paid amount for the specified price.
/// </summary>
/// <param name="price">An int indicating the price of the purchase.</param>
/// <param name="paid">An int indicating the total amount paid.</param>
private static void outputChange(int price, int paid)
{
    int change = paid - price;

    if (change > 0)
    {
        Console.Write("Change: ");
        while (change > 0)
        {
            int coin = 0;

            if (change >= 50)
                coin = 50;
            else if (change >= 20)
                coin = 20;
            else if (change >= 10)
                coin = 10;
            else if (change >= 5)
                coin = 5;
            else if (change >= 2)
                coin = 2;
            else if (change >= 1)
                coin = 1;

            if (coin > 0)
            {
                Console.Write($"{coin} ");
                change -= coin;
            }
            else
                break;
        }
        Console.Write("\n");
    }
    else
        Console.WriteLine("No change needed, you paid with the exact value.");
}
//
///输出指定价格的已支付金额的更改。
/// 
///表示购买价格的整数。
///一个整数,表示支付的总金额。
私有静态无效输出更改(整数价格,整数支付)
{
int change=已付价格;
如果(更改>0)
{
控制台。写入(“更改:”);
而(更改>0)
{
整数=0;
如果(更改>=50)
硬币=50;
否则如果(更改>=20)
硬币=20;
否则如果(更改>=10)
硬币=10;
否则如果(更改>=5)
硬币=5;
否则如果(更改>=2)
硬币=2;
否则如果(更改>=1)
硬币=1;
如果(硬币>0)
{
Console.Write($“{coin}”);
硬币;
}
其他的
打破
}
控制台。写入(“\n”);
}
其他的
WriteLine(“无需更改,您以准确的值支付。”);
}

关于如何在线实现这一点,有很多例子。我倾向于使用
while
循环,而不是
for
循环。通过为每种类型的硬币创建一个值,可以使用相同的while+for语句吗?我现在真的必须避免使用数组。@Nijeil我用第二种方法更新了答案,该方法使用
if
语句替换硬币值数组
Change: 50 5 1 
Change: 50 20 5 2 
Change: 10 2 2 
No change needed, you paid with the exact value.
/// <summary>
/// Ouput the change from the paid amount for the specified price.
/// </summary>
/// <param name="price">An int indicating the price of the purchase.</param>
/// <param name="paid">An int indicating the total amount paid.</param>
private static void outputChange(int price, int paid)
{
    int change = paid - price;

    if (change > 0)
    {
        Console.Write("Change: ");
        while (change > 0)
        {
            int coin = 0;

            if (change >= 50)
                coin = 50;
            else if (change >= 20)
                coin = 20;
            else if (change >= 10)
                coin = 10;
            else if (change >= 5)
                coin = 5;
            else if (change >= 2)
                coin = 2;
            else if (change >= 1)
                coin = 1;

            if (coin > 0)
            {
                Console.Write($"{coin} ");
                change -= coin;
            }
            else
                break;
        }
        Console.Write("\n");
    }
    else
        Console.WriteLine("No change needed, you paid with the exact value.");
}