【问题标题】:How to link user input from a string and int如何从字符串和 int 链接用户输入
【发布时间】:2019-05-31 09:34:18
【问题描述】:

我想创建一个应用程序,用户可以在其中输入商品和价格,然后显示最贵和最便宜的商品。

我是 C# 的新手,没有任何语言的编程经验,2.5 周前开始学习。我不明白如何将商品与价格联系起来然后进行计算。就我如何构建应用程序而言,一分钱还没有下降:(

        ItemInput();
        PricingInput();

    }


    private static void ItemInput()
    {
        Console.WriteLine("Add your items and price, once you're done, type 'end' ");
        AskForTheItem();
        AskForThePrice();
    }

    static void AskForTheItem()
    {

            while (itemPrice != "end")
            {
                Console.Write("Add your item:");
                string item = Console.ReadLine();
                Console.Write("Add the price:");
                int price = int.Parse(Console.ReadLine());
                itemPrice = Console.ReadLine();

            }
            Console.WriteLine("Add your item: ");
       name = Console.ReadLine();
        numberOfItems++;
    }
    static void AskForThePrice()
    {
        Console.WriteLine("Add the price: ");
        price = int.Parse(Console.ReadLine());
        numberOfPrice++;
    }
    private static void PricingInput()
    {
        Console.WriteLine($"The cheapest item is: {name} and the most expensive item is: {name} ");

【问题讨论】:

  • 您可以使用.Min().Max() 来完成此操作,但在此之前您需要将项目、价格存储在集合中,它可能是数组或列表等。重组您的程序以存储列表在 ItemDetails 中,使用类是最好的选择,这里有 2 个属性,即 ItemName 和 Price,然后对其执行最小和最大操作。如果有人试图回答你的问题,他/她需要先重构你的代码

标签: c#


【解决方案1】:
 private static void ItemInput()
    {
         PricingInput(AskForTheItem());        
    }

    static Dictionary<String, int> AskForTheItem()
    {

            var result = new Dictionary<String, int>();
            Console.WriteLine("Add your items and price, once you're done, type empty item ");
            while (1)
            {
                Console.Write("Add your item or keep empty :");
                string item = Console.ReadLine();
                if(String.IsNullOrEmpty(item)) return result;
                Console.Write("Add the price:");
                while(!int.TryParse(out var price, Console.ReadLine())                 Console.Write("Wrong price, please Add the price:");
                result.Add(item, price);
            }
    }

    private static void PricingInput(Dictionary<String,int> list)
    {
        String minItem = null;
        int minPrice = -1;
        String maxItem = null;
        int maxPrice = -1;
        foreach(var i in list) {
            if(price<0 || i.Value<price) { minItem = i.Key; minPrice=i.Value;}
            if( i.Value>price) { maxItem = i.Key; maxPrice=i.Value;}
        Console.WriteLine($"The cheapest item is: {1} and the most expensive item is: {2} ",minItem,maxItem); }

【讨论】:

  • 感谢您的回答。在 C# 中,while(1) 不会编译。
  • 此外,如果您在字符串前面使用 $,则 {} 之间的 int 值将是一个文字,因此您的 PricingInput 方法的最后一行将始终将最便宜的商品打印为 1,将最昂贵的商品打印为2. 见this answer
【解决方案2】:

欢迎来到 StackOverflow!您的问题似乎很广泛,但我认为您需要的是使用类进行面向对象编程的第一步。

您的代码似乎是一个好的开始,但要存储信息,您需要一些对象。
您可以使用类创建自己的对象。在这种情况下,您的班级将需要一个属性作为其名称,一个属性作为其价格。 定义类如下(你应该在新文件中这样做):

class MyItem{
    public string Name { get; set; }
    public int Price { get; set; }
}

然后在使用 AskForTheItems 方法以及可能还有 Main 方法的类的开头,您需要添加一个 List 来存储项目。你会这样做:

private static List<MyItem> items = new List<MyItem>();

对于获取项目的方法,您需要进行一些调整。您也不再需要numberOfItems-变量,因为您可以在需要时调用items.Count

private static void AskForTheItems()
{
    do
    {
        Console.WriteLine("Add your item:");
        // get the name
        Console.Write("Name: ");
        string name = Console.ReadLine();
        // get and convert the price
        Console.Write("Price: ");
        int price = int.Parse(Console.ReadLine());

        // create the item and fill it with the values
        MyItem item = new MyItem();
        item.Name = name;
        item.Price = price;

        // add the item to your list
        items.Add(item);

        // ask if the user want's to add another one
        Console.WriteLine("Again? (y/n)");
    }
    while (Console.ReadKey(true).Key != ConsoleKey.N);
}

使用 Linq 打印最便宜和最昂贵的内容相当容易(甚至可能有更简单/更好的方法)。

private static void PrintCheapestAndMostExpensive() {
    // get the first item where the price is the same as the minimum of all prices in the list
    MyItem cheapestItem = items.First(i => i.Price == items.Min(i2 => i2.Price));
    // get the first item where the price is the same as the maximum of all prices in the list
    MyItem mostExpensiveItem = items.First(i => i.Price == items.Max(i2 => i2.Price));

    Console.WriteLine($"The cheapest item is: {cheapestItem.Name} and the most expensive item is: {mostExpensiveItem.Name}");
}

我希望这会有所帮助。如果您有任何问题,请随时提问 - 我会尽力解释您还不了解的任何内容。

顺便说一句,有几件事我没有做/介绍,因为我觉得像这样的小程序解释起来有点太多了。目前,将输入字符串转换为 int(错误)时没有错误处理。还有更简单的方法来编写项目的初始化,但我认为这种方式更容易理解。
同样使用此代码,它总是打印价格最低的第一个项目。如果有两个价格都最低,这可能是个问题。
Linq 本身相当难(但非常重要),这就是为什么我强烈建议您阅读有关它的内容。我很乐意更详细地解释我在此处使用的 Linq,但由于它是一个巨大的话题,我不会详细介绍。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多