如何从列表中删除对象

发布于 2025-01-24 08:41:17 字数 2920 浏览 0 评论 0 原文

我与这个代码几个小时战斗。 我需要一个建议。 如何通过在控制台中输入行星名称来删除整个对象? 下一个问题。如何编写代码以告知用户他提供了错误的信息,例如,他代替质量而不是int写了字符串? 我将感谢一些技巧 这是我的代码...

public class Lad
{
    public string PlanetName { get; set; }

    public MyDate Szczegoly { get; set; }
}

public class MyDate
{
    public int masa { get; set; }
    public int sredni { get; set; }

}


class Program
{

    static void RemovePerson(List<Lad> _data, string planet)
    {
        var foundPerson = _data.FirstOrDefault(x => x.PlanetName == planet);
        if (foundPerson != null)
            _data.Remove(foundPerson);
    }

    static void End()
    {

        Environment.Exit(0);

    }
   
    static void Main()
    {
        
        Console.WriteLine("hej, dokonaj wyboru");
        string Continue;
        List<Lad> _data = new List<Lad>();
        string choice = Console.ReadLine();
        string uppchoice = choice.ToUpper();
        do
        {

            switch (uppchoice)
            {

                case "ADD":
                    Console.WriteLine("Podaj nazwe planety");
                    string name = Console.ReadLine();
                    Console.WriteLine("Podaj mase");
                    int masa = Convert.ToInt16(Console.ReadLine());
                    Console.WriteLine("Podaj srednice");
                    int sred = Convert.ToInt16(Console.ReadLine());
                    _data.Add(new Lad()
                    {
                        PlanetName = name,

                        Szczegoly = new MyDate
                        {
                            masa = masa,
                            sredni = sred

                        }
                    });
                    var json = System.Text.Json.JsonSerializer.Serialize(_data);
                    Console.WriteLine(json);
                    


                    break;
                case "SAVE":
                    string json2 = JsonConvert.SerializeObject(_data, Formatting.Indented);
                    File.AppendAllText(@"tescikkk.json", json2);
                    Console.WriteLine("plik zapisany");
                    break;
                case "LIST":

                    var path = @"tescikkk.json";
                    if (File.Exists(path))
                    {
                        string text = File.ReadAllText(path, System.Text.Encoding.UTF8);
                        Console.WriteLine(text);
                    }

                    break;
                case "DELETE":
                    
                    break;
                case "EXIT":
                        End();
                        break;
            }

            Console.Write("Czy chcesz powtórzyć? (Y/N) : ");
            Continue = Console.ReadLine();
        } while (Continue != "N" && Continue != "n");

    }

   
   
    
}

I fight with this code couple hours.
I need an advice.
how to remove the entire object by entering the planet name in the console?
Next question. How to write the code to inform the user that he gave the wrong information, e.g. in the place of mass he wrote a string instead of int?
I will be grateful for a few tips
This is my code...

public class Lad
{
    public string PlanetName { get; set; }

    public MyDate Szczegoly { get; set; }
}

public class MyDate
{
    public int masa { get; set; }
    public int sredni { get; set; }

}


class Program
{

    static void RemovePerson(List<Lad> _data, string planet)
    {
        var foundPerson = _data.FirstOrDefault(x => x.PlanetName == planet);
        if (foundPerson != null)
            _data.Remove(foundPerson);
    }

    static void End()
    {

        Environment.Exit(0);

    }
   
    static void Main()
    {
        
        Console.WriteLine("hej, dokonaj wyboru");
        string Continue;
        List<Lad> _data = new List<Lad>();
        string choice = Console.ReadLine();
        string uppchoice = choice.ToUpper();
        do
        {

            switch (uppchoice)
            {

                case "ADD":
                    Console.WriteLine("Podaj nazwe planety");
                    string name = Console.ReadLine();
                    Console.WriteLine("Podaj mase");
                    int masa = Convert.ToInt16(Console.ReadLine());
                    Console.WriteLine("Podaj srednice");
                    int sred = Convert.ToInt16(Console.ReadLine());
                    _data.Add(new Lad()
                    {
                        PlanetName = name,

                        Szczegoly = new MyDate
                        {
                            masa = masa,
                            sredni = sred

                        }
                    });
                    var json = System.Text.Json.JsonSerializer.Serialize(_data);
                    Console.WriteLine(json);
                    


                    break;
                case "SAVE":
                    string json2 = JsonConvert.SerializeObject(_data, Formatting.Indented);
                    File.AppendAllText(@"tescikkk.json", json2);
                    Console.WriteLine("plik zapisany");
                    break;
                case "LIST":

                    var path = @"tescikkk.json";
                    if (File.Exists(path))
                    {
                        string text = File.ReadAllText(path, System.Text.Encoding.UTF8);
                        Console.WriteLine(text);
                    }

                    break;
                case "DELETE":
                    
                    break;
                case "EXIT":
                        End();
                        break;
            }

            Console.Write("Czy chcesz powtórzyć? (Y/N) : ");
            Continue = Console.ReadLine();
        } while (Continue != "N" && Continue != "n");

    }

   
   
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉絮湮 2025-01-31 08:41:17

您需要提供一个行星名称才能删除。然后,您可以返回false,如果找不到星球:

static bool RemovePlanet(List<Lad> _data, string planet)
{
    var foundPlanet = _data.FirstOrDefault(x => x.PlanetName == planet);
    if (foundPlanet != null)
    {
        _data.Remove(foundPlanet);
        return true;
    }
    return false;
}

然后这样称呼:

case "DELETE":
    var planetName = Console.ReadLine();
    if (RemovePlanet(_data, planetName)
    {
        Console.WriteLine($"{planetName} was deleted."
    }
    else
    {
        Console.WriteLine($"Sorry, {planetName} wasn't found."
    }

You need to supply a planet name to be able to delete. You can then return false, if the planet isn't found:

static bool RemovePlanet(List<Lad> _data, string planet)
{
    var foundPlanet = _data.FirstOrDefault(x => x.PlanetName == planet);
    if (foundPlanet != null)
    {
        _data.Remove(foundPlanet);
        return true;
    }
    return false;
}

Then call it like this:

case "DELETE":
    var planetName = Console.ReadLine();
    if (RemovePlanet(_data, planetName)
    {
        Console.WriteLine(
quot;{planetName} was deleted."
    }
    else
    {
        Console.WriteLine(
quot;Sorry, {planetName} wasn't found."
    }

属性 2025-01-31 08:41:17

处理“通知”的一种方法是这样做:

static Lad RemovePerson(List<Lad> _data, string planet)
{
    Lad foundPerson = _data.FirstOrDefault(x => x.PlanetName == planet);
    if (foundPerson != null)
    {
        _data.Remove(foundPerson);
    }

    return foundPerson;
}

如果您返回非零对象,则可以找到并删除。无效的意思是“找不到”。

您也可以退还布尔值。

Java实际上更频繁地这样做。

请参阅:

公共e删除(int index)

公共布尔删除(对象O)

因此您最终会得到类似的东西:

           case "DELETE":
                
              Lad removeTry = RemovePerson(blah, blah);
              if (null == removeTry)
              {
                 Console.Writeline("Not found");
              }
              else
              {
                Console.Writeline("lad removed");
               }



                break;

One way to handle the "notify" is by doing this:

static Lad RemovePerson(List<Lad> _data, string planet)
{
    Lad foundPerson = _data.FirstOrDefault(x => x.PlanetName == planet);
    if (foundPerson != null)
    {
        _data.Remove(foundPerson);
    }

    return foundPerson;
}

if you return a non-null object, it was found and removed. null means "not found".

You could also return a boolean.

Java actually does this more often.

See :

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#remove-int-

public E remove(int index)

public boolean remove(Object o)

thus you'd end up with something like:

           case "DELETE":
                
              Lad removeTry = RemovePerson(blah, blah);
              if (null == removeTry)
              {
                 Console.Writeline("Not found");
              }
              else
              {
                Console.Writeline("lad removed");
               }



                break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文