【问题标题】:Why doesn't my while loop go through all the cases when the conditions are true当条件为真时,为什么我的 while 循环不遍历所有情况
【发布时间】:2022-01-08 02:29:49
【问题描述】:

我的 while 循环有问题,在下面链接的 png 中,你会看到我想要做什么。

如果日期是 5 3 0,它应该返回 CCCDG。出了点问题。我不能使用ForGoto,必须使用whiledo-while

using System;

namespace FlowChart
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string wprowadzoneLiczby = Console.ReadLine();
            int[] arr = Array.ConvertAll(wprowadzoneLiczby.Split(' '), new Converter<string, int>(int.Parse)); 
                
            while (arr[0]>0)
            {
                while (arr[2]>0 && arr[0]>0)
                {
                    arr[0] -= 1;
                    arr[2] -= 1;
                    Console.Write("C");
                }
                while (arr[2]<0)
                {
                    Console.Write("D");
                    while (arr[2]>0)
                    {
                        Console.WriteLine();
                        break;
                    }
                    while (arr[2]<=0)
                    {
                        Console.Write("G");
                        break;
                    }
                    break;
                }
                break;
            }
            while (arr[0]<0)
            {
                Console.Write("E");
                Console.Write("G");
                Console.WriteLine();
                break;
            }
        }
    }
}

【问题讨论】:

  • 一个 while 循环,它在末尾有一个无条件的中断,本质上只是一个 if 语句。
  • 5 3 0 的示例输入中,arr[2] 将等于 0。当您有条件要求 arr[2] 不相等时,为什么任何(内部)循环都会运行到 0?
  • 阅读 Eric Lippert 的 How to Debug Small Programs 可能会有所帮助
  • 好的,所以我有一个不同的问题,可能是一个愚蠢的问题,但是如果 while 内的 while 结束,它会回到第一个 while 或继续到下一个。
  • 我想强调学习如何使用调试器的重要性。而不是试图猜测程序在做什么,您实际上可以按照每个步骤进行操作。不幸的是,在教授编程时有时会忽略这一点。

标签: c# loops while-loop do-while


【解决方案1】:

感觉就像我在为你做作业:) (但我很无聊)

如果您使用“x”、“y”、“z”变量而不是数组,则更容易理解。 我敢肯定还有其他方法可以解决这个问题。尤其是流程图中的“z>0”部分。但这“很好”。

请注意,在您的代码中,最后一个循环可能永远不会退出,因为如果 x>y 在开始时可以使用 x>0 到达那里。

注意,我已将其分解为仅将数字作为字符串。只是我很懒,不想在编码时继续输入字符串。 我相信你可以根据需要重构那部分。

注意break 以避开该循环。

另请注意,这可能会以多种方式失败。即如果您输入的数字少于 3 个会怎样?

正如其他人所说... 了解如何使用调试器单步调试您的代码。 了解“单元测试”。这些将“断言”您为每个输入获得正确的输出。这意味着您可以只“运行测试”来检查哪些失败,而不必一遍又一遍地手动输入字符串。

        static void Main(string[] args)
        {
            V2("1 2 3");
            V2("-2 0 2");
            V2("1 -1 2");
            V2("2 2 0");
            V2("5 3 0");

            Console.ReadLine();
        }

        static void V2(string numbers)
        {
            Console.Write(numbers + " = ");
            string[] n = numbers.Split(' ');

            int x = int.Parse(n[0]);
            int y = int.Parse(n[1]);
            int z = int.Parse(n[2]);

            while (x > 0)
            {
                while (y > 0)
                {
                    x -= 1;
                    y -= 1;
                    Console.Write("C");
                }
                Console.Write("D");
                break;
            }
            if (x <= 0)
            {
                Console.Write("E");
            }

            if (x <= 0 || z <= 0)
            {
                Console.Write("G");
            }

            Console.WriteLine();
        }
    }

【讨论】:

  • 感谢您的意见。在编写此代码时,我假设给定的数据将正确输入,重要的是不要使用 arr [0] 而是分配给变量。但是,这是简码,我的主要重点是只使用 while 函数。几分钟后,我找到了正确的解决方案。我没想到有人会为我写东西。这也是我的第一篇文章,我不知道如何完成一个问题。我祝大家有个美好的一天。而我今天的教训是学会调试!
  • 不用担心。我们都在学习。 “作业”评论应该是个玩笑。可能翻译得不好。所以我也学到了一些东西。没有一个 cmets 旨在作为批评。只是观察。很高兴听到您取得了进展。
猜你喜欢
  • 1970-01-01
  • 2021-08-20
  • 2020-11-25
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 2015-12-02
  • 2017-03-15
  • 1970-01-01
相关资源
最近更新 更多