【问题标题】:Issues with looping code and do-while loops (c#)循环代码和 do-while 循环的问题 (c#)
【发布时间】:2018-12-01 01:38:44
【问题描述】:
static double calculateTotals(double a)
    {
        double transfee = a * .01;
        double total = a + transfee;
        return total;
    }

    static void Main(string[] args)
    {
        Console.WriteLine("How many dontations to process?");
        int donations = Convert.ToInt16(Console.ReadLine());
        int[] count = new int[] { donations + 1 };
        int ct = 1;
        int i = -1;
        do
        {
            Console.WriteLine("Enter name: ");
            string name = Console.ReadLine();
            Console.WriteLine("Enter donation amount: ");
            double amount = Convert.ToDouble(Console.ReadLine());
            double transfee = amount * .01;
            i++;
            ct = count[i += 1];
            Console.WriteLine(name + "\t" + amount + "\t" + transfee);
        } while (i < donations);
        Console.WriteLine("TOTALS:" + "\t" + calculateTotals(amount) + "\t" + transfee);
        Console.ReadLine();
    }
}

你好。我是编码的初学者,所以如果这是一次糟糕的尝试,我深表歉意。

我正在尝试制作一个应用程序来记录个人捐赠的金额,计算交易费用,并为每个人输出结果。最后,我正在创建最后一行输出,其中将说明总捐款和总交易费用。

我目前不确定如何将数组正确地实现到我的循环中,并且不确定循环是否总体上得到了优化。

再说一遍,我是初学者。我为这样的代码道歉,但我希望对这些事情进行一些澄清。

谢谢!

【问题讨论】:

  • 代码现在做了什么?你想让它做什么呢?

标签: c# loops do-while


【解决方案1】:

首先,您的数组声明语法错误。见this link

所以应该是int[] count = new int[donations+1];

其次,您需要在循环之外声明和实例化您的金额和转账变量。

        double transfee = 0.0F;
        double amount = 0.0F;
        do
        {
            ...
            amount = Convert.ToDouble(Console.ReadLine());
            transfee = amount * .01;
            ...
        } while (i < donations);

这些信息应该足以让您重新开始。既然您正在学习,我认为没有人会真正为您提供一个可以完成您想要弄清楚的工作的答案:)

【讨论】:

  • 谢谢,这似乎确实有帮助。但是,对于“ct = count[i += 1];”这一行,我不断收到错误消息“索引超出了数组的范围。”知道如何解决吗?我知道我的数组计数错误,但具体在哪里?
  • 如果您单步执行您的代码,您会发现i 在数组的上限(即donations + 1)之上递增。因此,如果您的数组设置为 count[4],那么 i 将达到 5 并导致异常。注意你的 while 循环是如何计数的。你的(i &lt; donations) 应该在while 旁边,还是在do 旁边?此外,在评估 idonations 时,您是否需要做任何额外的数学运算?再说一次,不是为您回答 - 只是帮助您到达那里。
  • 愚蠢的问题:你知道如何设置断点并单步执行你的代码来观察值吗?
【解决方案2】:

你的代码:

        int i = -1;

        do
        {
            ...

            i++;
            ct = count[i += 1];
            ...

        } while (i < donations);

你实际上是增加 i 两倍,然后从 count[i] 中获取值分配给 ct 变量

查看此示例:

        int[] count = new int[3];
        count[0] = 0;
        count[1] = 1;
        count[2] = 2;

        int i = -1;
        do
        {
            i++;
            int x = count[i += 1];
            Console.WriteLine(x);
        } while (i < 3);

会导致IndexOutOfRangeException

解释:

第一个循环:

i++;                   // i increased 1, so i = 0
int x = count[i += 1]; // i increased 1, so i = 1, then get count[1] assign to x, x is 1

第二次循环:

i++;                   // i increased 1, so i = 2
int x = count[i += 1]; // i increased 1, so i = 3, then get count[3] assign to x

count[3] 导致 IndexOutOfRangeException

类似 count[i += 1] 的东西会让你的代码更难维护,在我看来,你应该尽可能避免它,尽量写清楚

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多