尝试从 5 名员工中找出出价最高的员工

问题描述 投票:0回答:1

分配的重点是分配所有 5 个人的姓名和他们出价的次数(必须低于或为零)。然后,我需要获取这些姓名和号码并存储它们,以便它们可以显示为出价最高的员工以及他出价的金额。 例如:Jason 的出价最高为 63。

这是我尝试过的代码,一切正常,直到它比较最大的出价并每次都选择我的第一个出价者。我对此还很陌生,所以我不知道所有术语和细节,所以请像孩子一样跟我说话。 C# 代码:

using System;
class TopSeller
{
    private static void 

Main(string[] args)
    {
        string employee;
        int widgetsSold;

        //Employee #1

        Console.WriteLine("Hello,     Can you please enter your name: ");
         employee =Console.ReadLine();
        Console.WriteLine("Enter the number of widgets sold: ");
        widgetsSold = int.Parse(Console.ReadLine());
        if(widgetsSold > 100)
        {
            Console.WriteLine("Number has to be at or below 100. Re-enter Widgets sold: ");
            widgetsSold = int.Parse(Console.ReadLine());
        }

        //Employee #2
        int widgetsSold2;
        String employee2;
        Console.WriteLine("Hello, Can you please enter your name: ");
        employee2 = Console.ReadLine();
        Console.WriteLine("Enter the number of widgets sold: ");
        widgetsSold2 = int.Parse(Console.ReadLine());
        if (widgetsSold2 > 100)
        {
            Console.WriteLine("Number has to be at or below 100. Re-enter Widgets sold: ");
            widgetsSold2 = int.Parse(Console.ReadLine());
        }

        //Employee #3
        String employee3;
        int widgetsSold3;
        Console.WriteLine("Hello, Can you please enter your name: ");
        employee3 = Console.ReadLine();
        Console.WriteLine("Enter the number of widgets sold: ");
        widgetsSold3 = int.Parse(Console.ReadLine());
        if (widgetsSold3 > 100)
        {
            Console.WriteLine("Number has to be at or below 100. Re-enter Widgets sold: ");
            widgetsSold3 = int.Parse(Console.ReadLine());
        }

        //Employee #4
        int widgetsSold4;
        String employee4;
        Console.WriteLine("Hello, Can you please enter your name: ");
        employee4 = Console.ReadLine();
        Console.WriteLine("Enter the number of widgets sold: ");
        widgetsSold4 = int.Parse(Console.ReadLine());
        if (widgetsSold4 > 100)
        {
            Console.WriteLine("Number has to be at or below 100. Re-enter Widgets sold: ");
            widgetsSold4 = int.Parse(Console.ReadLine());
        }

        //Employee #5
        int widgetsSold5;
        String employee5;
        Console.WriteLine("Hello, Can you please enter your name: ");
        employee5 = Console.ReadLine();
        Console.WriteLine("Enter the number of widgets sold: ");
        widgetsSold5 = int.Parse(Console.ReadLine());
        if (widgetsSold5 > 100)
        {
            Console.WriteLine("Number has to be at or below 100. Re-enter Widgets sold: ");
            widgetsSold5 = int.Parse(Console.ReadLine());
        }

        //top seller and largest bid
        String TopSeller;
        int LargestBid = 0;


        //Comparison
        if (widgetsSold > widgetsSold5)
        {
            TopSeller = employee;
            LargestBid = widgetsSold;
        }
        else if(widgetsSold < widgetsSold5)
        {
                TopSeller = employee5;
                LargestBid = widgetsSold5;
        }
        else if(widgetsSold4 > widgetsSold5)
        {
            TopSeller = employee4;
            LargestBid = widgetsSold4;
        }
        else if(widgetsSold3 > widgetsSold4)
        {
            TopSeller = employee3;
            LargestBid = widgetsSold3;
        }
        else if(widgetsSold2 > widgetsSold3)
        {
            TopSeller = employee2;
            LargestBid = widgetsSold2;
        }
        else if(widgetsSold > widgetsSold2)
        {
            TopSeller = employee;
            LargestBid = widgetsSold;
        }
        Console.WriteLine(TopSeller + " had the largest sell of: " + LargestBid);
    }
}
c# if-statement switch-statement
1个回答
0
投票

所以首先考虑将相关数据保存在一个对象中。我们可以使用几种不同类型的对象(如果您对此感兴趣,请查看记录),但为了简单起见,让我们构建一个

Bid
类。

public class Bid {
    public string EmployeeName { get; }
    public int WidgetsSold { get; }

    public Bid(string employeeName, int widgetsSold)
    {
        EmployeeName = employeeName;
        WidgetsSold = widgetsSold;
    }
}

上面我们创建了一个

Bid
对象来将数据保存在一起。该对象有几个属性,我将这些属性标记为“获取”,因为一旦创建了出价,我们就不想更改它。然后有一个构造函数,它获取小部件的名称和数量并将它们存储到这些属性中。

现在我们可以创建几个这样的投标类别并轻松地相互比较。我们将建立一份出价清单,然后从中获得最高的出价。我们还将创建循环中的出价逻辑,这样我们就不会重复代码,并且可以轻松扩展到要捕获的不同数量的出价。

using System;
using System.Collections.Generic;

class TopSeller
{
    // Defines the number of times to ask for bids
    public const int NumberOfBids = 5;

    private static void Main(string[] args)
    {
        // Create the list that will hold our bids
        var bidList = new List<Bid>();

        for (int i = 0; i < NumberOfBids; i++)
        {
            
            var employeeName = ReadEmployeeName();
        
            var widgetsSold = ReadNumberOfWidgets();

            // Create a new Bid and add it to our list.
            bidList.Add(new Bid(employeeName, widgetsSold));
        }

        // Put the bids in order of largest number of widgets sold to smallest, and select the first one
        var maxBid = bidList.OrderByDescending(bid => bid.WidgetsSold).First();

        Console.WriteLine($"{maxBid.EmployeeName} had the max bid of {maxBid.WidgetsSold}");
    }

    private static string ReadEmployeeName()
    {
        Console.WriteLine("Hello, can you please enter your name: ");

        while (true) // We want to loop forever until someone enters a valid name
        {
            var name = Console.ReadLine()
            
            if (string.IsNullOrWhiteSpace(name))
            {
                Console.WriteLine("Name must not be empty, please try again");
            }
            else
            {
                return name;
            }
        }
    }

    private static int ReadNumberOfWidgets()
    {
        Console.WriteLine("Enter the number of widgets sold: ");

        while (true) // We want to loop forever until someone enters a valid number of widgets
        {
            var numberOfWidgetsString = Console.ReadLine()
            
            if (string.IsNullOrWhiteSpace(numberOfWidgetsString))
            {
                Console.WriteLine("Number of widgets must not be empty, please try again");
            }
            else
            {
                var success = int.TryParse(numberOfWidgetsString, out var numberOfWidgets);

                if (!success)
                {
                    Console.WriteLine("Please enter a whole number");
                }
                else if (numberOfWidgets < 0 || numberOfWidgets > 100)
                {
                    Console.WriteLine("Number of widgets must be between 0 and 100");
                }
                else
                {
                    return numberOfWidgets;
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.