-3

我有这个代码:

        Console.Write("Red = ");
        var input = Console.ReadLine();

        if (!Double.TryParse(input, out red))
        {
            Console.WriteLine("You have not entered an appropriate value!");
        }

        Console.Write("Green = ");
        input = Console.ReadLine();

        if (!Double.TryParse(input, out green))
        {
            Console.WriteLine("You have not entered an appropriate value!");
        }

        Console.Write("Green = ");
        input = Console.ReadLine();

        if (!Double.TryParse(input, out green))
        {
            Console.WriteLine("You have not entered an appropriate value!");
        }

我想更换相同的零件。
我创建了班级

public class ConverNumber
{
    public string InputString { get; private set; }
    public double Number { get; private set; }

    public static void ConvertNumber(string inputString, out double number)
    {
        if (!Double.TryParse(inputString, out number))
        {
            Console.WriteLine("You have not entered an appropriate value!");
        }
    }
}

但我不明白如何使用它?
而且我不确定也许我应该创建一个方法?

4

3 回答 3

5

如果您只是想调用该方法,则应替换以下代码:

Console.Write("Green = ");
input = Console.ReadLine();

if (!Double.TryParse(input, out green))
{
    Console.WriteLine("You have not entered an appropriate value!");
}

有了这个

Console.Write("Green = ");
input = Console.ReadLine();

ConverNumber.ConvertNumber(input, out green);
于 2013-07-05T14:06:33.387 回答
3

你可以像这样使用它:

Console.Write("Red = ");
input = Console.ReadLine();
ConverNumber.ConvertNumber(input, out red);

等等

于 2013-07-05T14:09:39.397 回答
0

我稍微改变了我的班级

public class ConverNumber
{
    public string InputString { get; private set; }
    public double Number { get; private set; }

    public void ConvertNumber(string inputString, out double number)
    {
        if (!Double.TryParse(inputString, out number))
        {
            Console.WriteLine("Вы не ввели значение!");
        }
    }
}

并使用了这段代码

        Console.Write("Red = ");

        var input = new ConverNumber();
        input.ConvertNumber(Console.ReadLine(), out red);

        Console.Write("Green = ");
        input.ConvertNumber(Console.ReadLine(), out green);

        Console.Write("Blue = ");
        input.ConvertNumber(Console.ReadLine(), out blue);
于 2013-07-05T14:11:42.667 回答