如果 - else构建不起作用

rinz.

成员
加入
4月9日,2020年4月
消息
21
编程经验
Beginner
我需要一名志愿者来审查C#代码。如果IF - ISEL INCLUCTS的几个问题仅在预期中不流动。编程很新。请指教。
 

rinz.

成员
加入
4月9日,2020年4月
消息
21
编程经验
Beginner
我正在热身达到使事情更简单所需的任务的复杂性。我喜欢用户响应提示的想法。我也想到了许多功能,我会增加,但是目前的技能缩短了。要求该应用程序作为控制台应用程序交付。我很欣赏你和我一起坚持下去。我期待为您提供更多了解更多信息。
我认为我可以获得更多关于如何改进应用程序的交互性质的更多想法。
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,893
地点
切萨皮克,va.
编程经验
10+
这很容易。这是一些伪代码:
C#:
class Settings
{
    public string InputFileName { get; set; }
    public string OutputFileName { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public int Red { get; set; } = -1;
    public int Green { get; set; } = -1;
    public int Blue { get; set; } = -1;
}

static Settings ParseArguments(string [] args)
{
    // your code here to parse the arguments array
    // recall, there is no need to prompt the user, all you do is look at the args values.
   // if there's a problem print out the issue and return null.
}

static string PromptForString(string prompt)
{
    // add code to handle empty strings
    Console.Write(prompt);
    var input = Console.ReadLine();
    return input;
}

static string PromptForInteger(string prompt, int min = int.MinValue, int max = int.MaxValue, bool acceptDefault = false, int defaultValue = 0)
{
    while (true)
    {
        Console.Write(prompt);
        var input = Console.ReadLine();

        if (string.IsNullOrWhiteSpace(input) && acceptDefault)
            return defaultValue;

        if (int.TryParse(input, out int value) && min <= value && value <= max)
            return value;

        Console.WriteLine($"Invalid input. Enter an integer that is between {min} and {max} (inclusive).");
    }
}

static int PromptForColorThreshold(string color)
{
    return PromptForInteger($"What is the {color} color threshold? (0-255, just press Enter for default value of -1)",
                                        min: 0, max: 255, acceptDefault : true, defaultValue : -1);
}

static Settings PromptForSettings()
{
    return new Settings()
    {
        OutputFileName = PromptForString("What is the input bitmap file name? ");
        InputFileName = PromptForString("What is the output text file name? ");
        Width = PromptForInteger("What is the width of the output image?");
        Height = PromptForInteger("What is the height of the output image?");
        Red = PromptForColorThreshold("red");
        Green = PromptForColorThreshold("green");
        Blue = PromptForColorThreshold("blue");
    };
}

static int Main(string [] args)
{
    string inputFileName;
    string outputFileName;
    int width;
    int height;
    int redThreshold = -1;
    int greenThreshold = -1;
    int blueThreshold = -1;

    var settings = args.Length == 0 ? PromptForSettings() : ParseArguments(args);
    if (settings == null)
        return -1;

    var app = new ImageBinarizerApp();
    app.Binarize(settings.InputFileName, settings.OutputFileName, settings.Width, settings.Height,
                    settings.Red, settings.Green, settings.Blue);
}

The key is in the various PromptFor*() methods. They simply show the user what input is expected, and wait for the user input. The prompt for integer does a bit more error checking to make sure that user enters a value that is within the minimum and maximum range. It also has the allowDefault flag that lets the user not provide any input value, and the passed in defaultValue will be returned.
 
最佳 底部