C# 如果else构造失败(.NET Framework)
我正在开发一个二进制图像制作应用程序。我对主方法算法有困难。程序总是在提供第一个参数('-help')后终止。它不按所描述的顺序流动。我认为if-else构造有缺陷,但我无法理解。有人能看一下并提出建议吗C# 如果else构造失败(.NET Framework),c#,.net,visual-studio,image-processing,C#,.net,Visual Studio,Image Processing,我正在开发一个二进制图像制作应用程序。我对主方法算法有困难。程序总是在提供第一个参数('-help')后终止。它不按所描述的顺序流动。我认为if-else构造有缺陷,但我无法理解。有人能看一下并提出建议吗 namespace BinaryImage_Console { /// <summary> /// Program Class of Console App /// </summary> class Program {
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();
}
}
}
}
名称空间二进制图像\u控制台
{
///
///控制台应用程序的程序类
///
班级计划
{
///
///程序的主要入口点
///
///主方法论元
静态void Main(字符串[]参数)
{
Console.WriteLine(“欢迎使用二进制图像生成器”);
Console.WriteLine(“\n使用以下命令获取帮助:”);
WriteLine(“dotnetimagebinarizerapp-help”);
args=新字符串[]{Console.ReadLine()};
//测试是否提供了必要的输入参数。
如果(参数长度<8)
{
如果(args.Length==1&&args[0]。等于(“-help”))
{
Console.WriteLine(“\n帮助:”);
Console.WriteLine(“\n按如下方式处理参数:”);
Console.WriteLine(\n带有自动RGB的示例:\nDTNET ImageBinarizerApp--输入图像c:\\a.png--输出图像d:\\out.txt-宽度32-高度32”);
Console.WriteLine(\n带有显式RGB的示例:\nDTNET ImageBinarizerApp——输入图像c:\\a.png——输出图像d:\\out.txt——宽度32——高度32——红色100——绿色100——蓝色100”);
}
其他的
{
Console.WriteLine(“\n错误:未传递所有必需的参数。请先传递参数。”);
}
Console.WriteLine(“\n按任意键退出应用程序”);
Console.ReadLine();
回来
}
其他的
{
字符串inputImagePath=“”;
字符串outputImagePath=“”;
int imageWidth=0;
int-imageHeight=0;
int redThreshold=-1;
int绿色阈值=-1;
int-blueThreshold=-1;
如果(args[0].Equals(“--input image”)&&File.Exists(args[1]))
{
inputImagePath=args[1];
}
其他的
{
Console.WriteLine(“\n错误:输入文件不存在。”);
Console.WriteLine(“\n按任意键退出应用程序”);
Console.ReadLine();
回来
}
int separatorIndex=args[3].LastIndexOf(Path.directorySparatorChar);
如果(args[2].Equals(“--output image”)&&separatorIndex>=0&&Directory.Exists(args[3].Substring(0,separatorIndex)))
{
outputImagePath=args[3];
}
其他的
{
Console.WriteLine(“\n错误:输出目录不存在。”);
Console.WriteLine(“\n按任意键退出应用程序”);
Console.ReadLine();
回来
}
如果(!args[4]。等于(“-width”)| |!int.TryParse(args[5],out-imageWidth))
{
Console.WriteLine(“\n错误:图像宽度应为整数。”);
Console.WriteLine(“\n按任意键退出应用程序”);
Console.ReadLine();
回来
}
如果(!args[6]。等于(“-height”)| |!int.TryParse(args[7],out-imageHeight))
{
Console.WriteLine(“\n错误:图像高度应为整数。”);
Console.WriteLine(“\n按任意键退出应用程序”);
Console.ReadLine();
回来
}
如果(参数长度>8)
{
如果(参数长度<14)
{
Console.WriteLine(“\n错误:应通过所有三个红色、绿色和蓝色阈值。”);
Console.WriteLine(“\n按任意键退出应用程序”);
Console.ReadLine();
回来
}
其他的
{
如果(!args[8]。等于(“-red”)| |!(int.TryParse(args[9],out redThreshold))| | redThreshold<0 | | redThreshold>255)
{
Console.WriteLine(“\n错误:红色阈值应在0和255之间。”);
Console.WriteLine(“\n按任意键退出应用程序”);
Console.ReadLine();
回来
}
如果(!args[10]。等于(“-green”)| |!(int.TryParse(args[11],out greenThreshold))| | greenThreshold<0 | | greenThreshold>255)
{
Console.WriteLine(“\n错误:绿色阈值应在0和255之间。”);
Console.WriteLine(“\n按任意键退出应用程序”);
Console.ReadLine();
回来
}
如果(!args[12]。等于(“-blue”)| |!(int.TryParse(args[13],out blueThreshold))| | blueThreshold<0 | | blueThreshold>255)
{
Console.WriteLine(“\n错误:蓝色阈值应在0和255之间。”);
Console.WriteLine(“\n按任意键退出应用程序”);
Console.ReadLine();
回来
}
}
}
"Error: All necessary arguments are not passed. Please pass the arguments first."
args = new String[] { Console.ReadLine() };