Listy oraz wczytywania znaków z konsoli

0

Witam mam kilka pytań:

  1. Jak zrobić aby do listy można było dodawać nowe osoby do listy przez użytkownika? Próbowałem na różne sposoby ale nie działało.
  2. Jak wczytać znak z konsoli? Chodzi mi o to, żę jak nacisnę ESC czyli 27 to mi zakończy, a jak co innego to będzie kontynuować pętle.
namespace ConsoleApplication12
{
    class Program
    {
        static void Main(string[] args)
        {
     
                var Dane2 = new List<Lista>();
                    
                    
                foreach (var Lista1 in Dane2)
                {
                    if (Dane2 == null) //tu chciałem, że jak lista jest pusta to wypisuje komunikat "pusto", ale nie działa 
                        WriteLine("Pusto");
                    else
                    {
                        WriteLine(Formatowanie.Format(Lista1));
                     
                    }
                }

                WriteLine("Dodac nowego obywatela?");

            ReadKey();
        }
    }
}

------------klasa1-----------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication12
{
    class Formatowanie
    {

        public static string Format(Lista dane)
        {
            return dane.Imie;
        }

    }
}


---------------klasa2------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication12
{
    public class Lista
    {

        public string Imie { get; set; }

        public Lista(string imie)
        {
            Imie = imie;
        }

    }
}
0

Podpowiem

  1. Listy posiadają ciekawą metodę Add.
  2. Metoda ReadKey.
0

Jakaś dodatkowa podpowiedź jak to dać do pętli?

0

Próbuje używać tych metod Add() i ReadKey() ale w żaden sposób nie jestem w stanie tego zrobić. Ciągle wyskakuje mi jakiś błąd. Mógłby mi ktoś to opisać jak to zrobić? Sam już nie daje rady.

0

Poratuje ktoś rozwiązaniem? Czymkolwiek?

0

Chodzi o to, że nie mam pojęcia jak to zrobić.
Próbowałem tak:

var button; // tu mam bład implicitly-typed local variables must be initialized

do
{

var t = ReadKey();

                if (t != 116 ) // tu blad ze nie moge uzyc !=
                {
                    WriteLine(":D");
                }
                else
                {
                    WriteLine(":(");
                }

WriteLine("Nacisnij ESC aby zakonczyc);
button = ReadKey();

}
while (button =! 27)

przy Dane2.Add(); nie wiem jak podać to w pętli aby użytkownik mógł dodać osobę chcialem jakoś przez ReadLine(); ale było napisane cannot convert string

0

To jak to zrobić? co podać do Dane2.Add(); i w jaki sposób tak samo do pętli.
przy tym drugim dalem

 if (t.Key.ToString() == "t" )
                {
                    WriteLine(":D");
                }
                else
                {
                    WriteLine(":(");
                }

tylko teraz jak wciskam t to wypisuje mi ;( zamiast :D

0

Zrobiłem tak i działa uff...

var Dane2 = new List<Lista>()
            {
            };
            ConsoleKeyInfo info;
            do
            {
               
                if (Dane2.Count != 0)
                {
                    foreach (var Lista1 in Dane2)
                    {
                        WriteLine(Formatowanie.Format(Lista1));
                    }

                }
                else
                {
                    WriteLine("Lista jest pusta");
                }

                WriteLine("Dodac nowa osobe? t/n");
                ConsoleKeyInfo t = ReadKey();

                if (t.Key == ConsoleKey.T )
                {
                    WriteLine(":D");
                }
                WriteLine("\n Nacisnij esc aby zakonczyc, aby kontunuwac nacjsnij dowolny");
                info = Console.ReadKey();

            } while (info.Key != ConsoleKey.Escape);
       
            ReadKey();

Potrzebuje jeszcze pomocy z tym Dane2.Add();

0

Rozwiązałem problem z ADD


do
            {
               
                if (Dane2.Count != 0)
                {
                    foreach (var Lista1 in Dane2)
                    {
                        WriteLine(Formatowanie.Format(Lista1));
                    }

                }
                else
                {
                    WriteLine("Lista jest pusta");
                }

                WriteLine("Dodac nowa osobe? t/n");
                ConsoleKeyInfo t = ReadKey();

                if (t.Key == ConsoleKey.T )
                {
                    string Imie = Console.ReadLine();
                    Dane2.Add(new Lista(Imie));
                }
                else
                {
                    WriteLine("\nNie to nie :)");
                }
                WriteLine("\nNacisnij esc aby zakonczyc, aby kontunuwac nacjsnij dowolny");
                info = Console.ReadKey();

            } while (info.Key != ConsoleKey.Escape);
       
            ReadKey();

1 użytkowników online, w tym zalogowanych: 0, gości: 1