【问题标题】:If-else construct failing (.NET Framework)If-else 构造失败(.NET Framework)
【发布时间】:2020-07-29 21:57:29
【问题描述】:

我正在开发二进制图像制作应用程序。我对主要方法算法有困难。程序总是在提供第一个参数 ('-help') 后终止。它不像描述的那样按顺序流动。我认为 if-else 构造有问题,但我无法弄清楚。有人可以看看和建议吗?

namespace BinaryImage_Console
{
    /// <summary>
    /// Program Class of Console App
    /// </summary>
    class Program
    {
        /// <summary>
        /// Main entry point for Program
        /// </summary>
        /// <param name="args">Argument of main method</param>
        static void Main(string[] args)
        {

            Console.WriteLine("Welcome to Binary Image Maker");            
            Console.WriteLine("\nUse following command for help:");
            Console.WriteLine("dotnet ImageBinarizerApp -help");
            args = new String[] { Console.ReadLine() };

            //Test if necessary input arguments were supplied.
            if (args.Length < 8)
            {
                if(args.Length == 1 && args[0].Equals("-help"))
                {
                    Console.WriteLine("\nHelp:");
                    Console.WriteLine("\nPass the arguments as following:");
                    Console.WriteLine("\nExample with automatic RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image d:\\out.txt -width 32 -height 32");
                    Console.WriteLine("\nExample with explicit RGB:\ndotnet ImageBinarizerApp --input-image c:\\a.png --output-image d:\\out.txt -width 32 -height 32 -red 100 -green 100 -blue 100");
                }
                else
                {
                    Console.WriteLine("\nError: All necessary arguments are not passed. Please pass the arguments first.");
                }
                Console.WriteLine("\nPress any key to exit the application.");
                Console.ReadLine();
                return;
            }
            else
            {
                String inputImagePath = "";
                String outputImagePath = "";
                int imageWidth = 0;
                int imageHeight = 0;
                int redThreshold = -1;
                int greenThreshold = -1;
                int blueThreshold = -1;

                if(args[0].Equals("--input-image") && File.Exists(args[1]))
                {
                    inputImagePath = args[1];
                }
                else
                {
                    Console.WriteLine("\nError: Input file doesn't exist.");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                int separatorIndex = args[3].LastIndexOf(Path.DirectorySeparatorChar);
                if (args[2].Equals("--output-image") && separatorIndex >= 0 && Directory.Exists(args[3].Substring(0, separatorIndex)))
                {
                    outputImagePath = args[3];
                }
                else
                {
                    Console.WriteLine("\nError: Output Directory doesn't exist.");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                if (!args[4].Equals("-width") || !int.TryParse(args[5], out imageWidth))
                {
                    Console.WriteLine("\nError: Image Width should be integer.");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                if (!args[6].Equals("-height") || !int.TryParse(args[7], out imageHeight))
                {
                    Console.WriteLine("\nError: Image Height should be integer.");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                if(args.Length > 8)
                {
                    if(args.Length < 14)
                    {
                        Console.WriteLine("\nError: All three Red, Green and Blue Thresholds should be passed.");
                        Console.WriteLine("\nPress any key to exit the application.");
                        Console.ReadLine();
                        return;
                    }
                    else
                    {
                        if (!args[8].Equals("-red") || !(int.TryParse(args[9], out redThreshold)) || redThreshold < 0 || redThreshold > 255)
                        {
                            Console.WriteLine("\nError: Red Threshold should be in between 0 and 255.");
                            Console.WriteLine("\nPress any key to exit the application.");
                            Console.ReadLine();
                            return;
                        }

                        if (!args[10].Equals("-green") || !(int.TryParse(args[11], out greenThreshold)) || greenThreshold < 0 || greenThreshold > 255)
                        {
                            Console.WriteLine("\nError: Green Threshold should be in between 0 and 255.");
                            Console.WriteLine("\nPress any key to exit the application.");
                            Console.ReadLine();
                            return;
                        }

                        if (!args[12].Equals("-blue") || !(int.TryParse(args[13], out blueThreshold)) || blueThreshold < 0 || blueThreshold > 255)
                        {
                            Console.WriteLine("\nError: Blue Threshold should be in between 0 and 255.");
                            Console.WriteLine("\nPress any key to exit the application.");
                            Console.ReadLine();
                            return;
                        }
                    }                    
                }
                else
                {
                    redThreshold = -1;
                    greenThreshold = -1;
                    blueThreshold = -1;
                }

                Console.WriteLine("\nImage Binarization in progress...");

                try
                {
                    ImageBinarizerApplication obj = new ImageBinarizerApplication();
                    obj.Binarizer(inputImagePath, outputImagePath, imageWidth, imageHeight, redThreshold, greenThreshold, blueThreshold);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"\nError: {e.Message}");
                    Console.WriteLine("\nPress any key to exit the application.");
                    Console.ReadLine();
                    return;
                }

                Console.WriteLine("\nImage Binarization completed.");
                Console.WriteLine("\nPress any key to exit the application.");

                Console.ReadLine();
            }            
        }
    }
}

【问题讨论】:

  • 您希望args = new String[] { Console.ReadLine() }; 将单个字符串拆分为多个字符串吗?
  • 你应该使用像CommandLineParser这样的开源库archive.codeplex.com/?p=commandline
  • @IłyaBursov 谢谢。我希望通过args = new String[] { Console.ReadLine() }; 实现这一目标。你有什么建议?
  • 如果你想使用命令行参数 - 只需使用已经存在的输入数组args,如果你想从用户那里读取数据,那么你需要解析它,比如docs.microsoft.com/en-us/archive/msdn-magazine/2019/march/…
  • 谢谢@IłyaBursov。我能够解决这个问题。您的链接有所帮助。

标签: c# .net visual-studio image-processing


【解决方案1】:

我猜如果您没有输入 -help 作为参数,您将收到消息:

"Error: All necessary arguments are not passed. Please pass the arguments first."

因为您将 args 分配为:

args = new String[] { Console.ReadLine() };

您的args.Length 将永远是equal to 1

【讨论】:

  • 我怎样才能参考args.Length 来解释参数。
  • 不要手动将它们分配给 args,args 是来自外部的参数(如果您将其作为命令行运行,则可以传递它)。另一种方法是使用 Console.ReadLine() 并将输入分配给数组/变量并使用它。
猜你喜欢
  • 2017-10-27
  • 2022-01-07
  • 2018-09-08
  • 1970-01-01
  • 2011-06-26
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多