如何修复在相同输入下无法运行两次的循环

时间:2019-04-19 01:17:24

标签: c# math

我正在尝试制作一个简单的程序,要求一个整数,如果该整数是质数,则输出其因子。如果用户的输入不是主要因素,它将要求用户输入主要因素。问题是,如果我连续两次输入一个有效的质因子,它不会给我正确的输出。我已经为此工作了一段时间,我认为我很想念某些明显的东西,因为我无法弄清楚。

class Program
{
    static void Main(string[] args)
    {
        List<int> factors = new List<int>();
        int a, b, c;
        Console.Write("Hello,\n Please enter an integer: ");
        string userInput = Console.ReadLine();
        while (userInput != "quit")
        {
            try
            {
                a = int.Parse(userInput);
                c = a;
                bool negative = a < 0;
                int letter = Convert.ToInt32(Int32.TryParse(userInput, out letter));
                if (!negative && letter != 0)
                {
                    for (b = 2; a > 1;)
                        if (a % b == 0)
                        {                
                            while (c % b == 0)
                            {
                                c /= b;
                                factors.Add(c);
                            }
                            Console.WriteLine($"{a} has factors: { String.Join(", ", factors)}");
                            Console.Write("Please enter another integer: ");
                            factors.Clear();
                            userInput = Console.ReadLine();
                            a = int.Parse(userInput);


                        }
                }
                else
                {
                    Console.Write("Please enter a valid prime factor: ");
                    userInput = Console.ReadLine();
                }

            }
            catch
            {
                Console.Write("Please enter a valid prime factor: ");
                userInput = Console.ReadLine();
            }
        }
    }
}

你好,  请输入一个整数:64 ...
64具有因子:32、16、8、4、2、1 请输入另一个整数:y ...
请输入有效的质数:

现在我是否连续执行两个有效素数

你好,  请输入一个整数:64 ...
64具有因子:32、16、8、4、2、1 请输入另一个整数:8 ...
8有因素: 请输入另一个整数:

但是。...

你好,  请输入一个整数:64 ...
64具有因子:32、16、8、4、2、1 请输入另一个整数:y ...
请输入有效的质数:8 8个因素:4、2、1 请输入另一个整数:

1 个答案:

答案 0 :(得分:1)

通常我不会提供答案,因为对于开发人员来说,能够自己解决这些问题很重要。但是我无法在评论中回应。因此,我将提供一些反馈,使我找到解决方案,并提供一些一般性意见。

如果您花时间调试,您将能够在代码中看到问题。添加简单的Console.WriteLines来指示在代码流中哪里出错了。

您还需要为变量提供更好的名称。 a,b,c毫无意义,难以理解其目的。更好的变量名意味着我们可以更好地理解您所遇到的问题,以及代码正在尝试执行的操作。编写良好的代码块应在5秒钟内可读且一般可理解。我已尽力将其总体上应用到您的代码中,而无需进行过多的重构,因此您可以在比较中看到代码中的冗余点。

问题是,当我调试您的代码时,似乎发生了无限循环或int解析有些奇怪。因为我不想花太多时间了解发生这种情况的原因,所以我对代码提出了以上建议,以查看是否仅通过降低代码复杂性和提高可读性就可以减少发生错误的风险。

下面的解决方案可能与您追求的目标并非100%一致,基于缺乏预期结果很难知道。

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        List<int> factors = new List<int>();
        while (true)
        {
            Console.WriteLine("Please enter an integer: ");
            var userInput = Console.ReadLine();
            if (userInput.Equals("quit"))
                break;

            if (!Int32.TryParse(userInput, out var firstOut)
               || !Int32.TryParse(userInput, out var secondOut))
                continue; 

            if (firstOut % 2 != 0 || firstOut < 0)
                continue;

            while (secondOut % 2 == 0)
            {
                secondOut /= 2;
                factors.Add(secondOut);
            }

            Console.WriteLine($"{firstOut} has factors: {String.Join(", ", factors)}");
            factors.Clear();
        }
    }
} 
相关问题