从c#中的列表调用

时间:2017-09-25 21:19:34

标签: c# list

我试图向用户显示他们所制作的电影的选择,但是,我不确定如何从列表中调用它。

public class Program
{
    static void Main(string[] args)
    {
        var movieList = new List<Movies>();

        //List of movies        
        movieList.Add ( new Movies {
        movieName = "Back to the Future",
        duration = 116});

        //Display all movies in list
        int i = 0;
        foreach (var Movie in movieList)
        {
            Console.WriteLine("{0}) Name: {1} | Duration: {2} mins",i, Movie.movieName, Movie.duration);
            i++;
        }

        //Selection of movie
        Console.Write("Please select a movie: ");
        var userMovieSelection = Console.ReadKey().KeyChar;

        Console.WriteLine("You've selected: {0} | {1} mins", [I'm having an issue here]

        Console.ReadKey(true);   
    }
}

public class Movies
{
    public string movieName {get; set;}
    public int duration {get; set;}
}

我已尝试movielist.movieName[userMovieSelection]之类的内容,但不断出现错误,例如没有定义。

4 个答案:

答案 0 :(得分:0)

KeyChar将成为一个角色或字符串。

if #available(iOS 10.0, *) {           
    UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
        print("here are the iOS 10 notifs \(requests)")
    }         
} else {
    let requests = (UIApplication.shared.scheduledLocalNotifications ?? [])
    print("here are the NON iOS 10 notifs \(requests)")
}

答案 1 :(得分:0)

您可以使用以下内容:

        var isNumber = int.TryParse(Console.ReadLine(), out var value);
        if (isNumber && value >= 0 && value < movieList.Count)
        {
            var userMovieSelection = Convert.ToInt32(value);
            var selectedMovie = movieList[userMovieSelection];
            Console.WriteLine("You've selected: {0} | {1} mins", selectedMovie.movieName, selectedMovie.duration);
        }

这将从列表中选择所选的电影。完整的代码如下。

更新1 - 包括检查用户是否输入有效号码

更新2 - 包括检查用户是否输入现有的电影号码

public class Program
{
    static void Main(string[] args)
    {
        // You can initialise movie list
        var movieList = new List<Movie>
        {
            new Movie
            {
                movieName = "Back to the Future",
                duration = 116
            }
        };

        //Display all movies in list
        int i = 0;
        foreach (var movie in movieList)
        {
            Console.WriteLine("({0}) Name: {1} | Duration: {2} mins", i, movie.movieName, movie.duration);
            i++;
        }

        //Selection of movie
        Console.Write("Please select a movie: ");

        var isNumber = int.TryParse(Console.ReadLine(), out var value);
        if (isNumber && value >= 0 && value < movieList.Count)
        {
            var userMovieSelection = Convert.ToInt32(value);
            var selectedMovie = movieList[userMovieSelection];
            Console.WriteLine("You've selected: {0} | {1} mins", selectedMovie.movieName, selectedMovie.duration);
        }
        else
        {
            Console.WriteLine("Incorrect value entered");
        }
        Console.ReadKey(true);
    }
}

关于您的代码的一些注意事项:

  • 我将Movies类名重命名为Movie,因为它只显示1部电影
  • 在foreach循环中,您需要调用影片变量,而不是影片类来获取名称和持续时间
  • 要阅读用户编号输入,最好使用Console.ReadLine()

答案 2 :(得分:0)

Console.WriteLine(“你选择了:{0} | {1}分钟”);

表示什么都没有,{0}和{1}不存在,如果它们不在foreach中,你需要调用数组索引来调用List,这是代码以正确的方式执行:

ConsoleKeyInfo userMovieSelection = Console.ReadKey();

int userChoice = 0;

if (Int32.TryParse(userMovieSelection.KeyChar.ToString(), out userChoice))
{
    Console.WriteLine("You've selected: " + movieList[userChoice].movieName + " | " + movieList[userChoice].duration + " mins");
}

答案 3 :(得分:-2)

char userMovieSelection = Console.ReadKey().KeyChar;
int movieIndex = userMovieSelection - '0';

使用movieIndex访问您的列表。当你是新人时,在var上轻松一下。