使用Method并将值传递给main方法

时间:2015-01-19 19:51:40

标签: c#

所以我有一个问题要求一个Movies方法,接受一个名字作为字符串和一个整数(代表时间以分钟为单位)。因此,如果在没有整数分钟的情况下调用此方法,则将其设置为默认值90.然后main方法显示您可以仅使用字符串调用movies方法,并显示您可以使用字符串和整数调用它。

static void Main()
{
    Console.WriteLine("Movie title is {0}, and time is", MovieListing("x") );
}

private static string MovieListing(string name, int defaultTime = 60)
{
    string Name, stringTime;
    int time;
    bool isValid;
    Console.Write("What is the name of the movie?");
    Name = Console.ReadLine();
    Console.Write("What is the time of the movie? Default is 90 if left blank");
    stringTime = Console.ReadLine();
    time = Convert.ToInt32(stringTime);           
}

所以我留下了空白思考如何让程序告诉用户是否输入了一个时间int并使用它或者如果他们只是使用默认的90并将它们传回主方法抱歉如果代码乱七八糟的话不同的方式没有太大的成功

3 个答案:

答案 0 :(得分:4)

您可以使用三元操作检查输入的值是否为空:

time = string.IsNullOrEmpty(stringTime) ? defaultTime  : Convert.ToInt32(stringTime); 

它将解析stringTime如果填写,否则它将采用默认值。

答案 1 :(得分:2)

这是一种更好的方法,使用简单的Movie对象来保存值。这应该打印出两个硬编码的行进行演示,然后要求用户输入:

private class Movie 
{
    private readonly int _defaultLength = 90;

    public string Title { get; set; }
    public int Length { get; set; }

    // constructor without length - use default length
    public Movie(string title)
    {
        this.Title = title;
        this.Length = _defaultLength;
    }

    // constructor with both properties
    public Movie(string title, int length)
    {
        this.Title = title;

        // make sure Length is valid
        if (length > 0)
            this.Length =length;
        else
            this.Length = _defaultLength;
    }
}

static void Main()
{
    // make a Movie object without length
    var shortMovie = new Movie("Zombieland");

    // make a Movie object and specify length
    var longMovie = new Movie("Lawrence of Arabia", 216);

    Console.WriteLine("{0} is {1} minutes long", shortMovie.Title, shortMovie.Length);
    Console.WriteLine("{0} is {1} minutes long", longMovie.Title, longMovie.Length);

    // get input from user: title
    Console.Write("What is the name of another movie?");
    var userTitle = Console.ReadLine();

    // get input from user: length
    Console.Write("What is the length of {0}? Default is 90 if left blank", userTitle);
    var userTime = Console.ReadLine();

    // try to convert user input to an int and call Movie constructor to create a new object
    int userTimeConverted = 0;
    Movie userMovie;
    if (Int32.TryParse(userTime, out userTimeConverted))
    {
        // make a new Movie object with the user's input
        userMovie = new Movie(userTitle, userTimeConverted);
    }
    else
    {
        // make a new Movie object without the user's input
        userMovie = new Movie(userTitle);
    }

    Console.WriteLine("{0} is {1} minutes long", longMovie.Title, longMovie.Length);
}

通过构建单独的Movie对象,我们可以移动存储默认长度所需的大部分或全部逻辑,并检查长度是否在Main()方法之外有效。这应该更清晰,更容易阅读。

作为额外的奖励,我们可以根据需要创建尽可能多的Movie实例 - 如上面的代码所示。

答案 2 :(得分:0)

将默认值设置为您不希望用户输入并检测和处理的值。

private static string MovieListing(string name, int defaultTime = -1)
{
    var isDefaultUsed = false;
    if (defaultTime = -1)
    {
       isDefaultUsed = true;
       defaultTime = 60;
    }
    string Name, stringTime;
    int time;
    bool isValid;
    Console.Write("What is the name of the movie?");
    Name = Console.ReadLine();
    Console.Write("What is the time of the movie? Default is 90 if left blank");
    stringTime = Console.ReadLine();
    time = Convert.ToInt32(stringTime);           
}