问题  是否有人对解决任务有不同的建议?

佐城

成员
已加入
2020年7月22日
留言内容
13
编程经验
Beginner
大家好。我正确地解决了任务。让我担心的是我的解决方案是否占用了太多内存。如果有人知道更好的解决方案,请写信。我目前正在研究复杂的条件和条件语句。
这是任务的文本。
这家商店出售三种类型的巧克力。对于每种类型的巧克力,我们都知道有多少克以及花费多少。编写一个C#程序,为用户从每种巧克力中加载巧克力的名称和价格,并确定哪种巧克力最具成本效益。最具成本效益的巧克力就是以最少的钱获得最多的巧克力。
这是示例(很抱歉用Excel编写,但是对我来说这是最简单的方法)
picture1.png



这是我的代码
C#:
namespace aplikacija
{
    class Program
    {
        static void Main(string[] args)
        {
           
            string chocolate1, chocolate2, chocolate3;
            int weight1, weight2, weight3, price1, price2, price3;

            Console.WriteLine(" \a  Hello everybody, today we will choose which chocolate you will buy :D \n  Enter information about chocolates\n Enter the names of the chocolates ");
            chocolate1 = Console.ReadLine();
            chocolate2 = Console.ReadLine();
            chocolate3 = Console.ReadLine();
            Console.WriteLine(" Enter the weight of the chocolates ");
            weight1 = int.Parse(Console.ReadLine());
            weight2 = int.Parse(Console.ReadLine());
            weight3 = int.Parse(Console.ReadLine());
            Console.WriteLine(" Enter the prices of the chocolates");
            price1 = int.Parse(Console.ReadLine());
            price2 = int.Parse(Console.ReadLine());
            price3 = int.Parse(Console.ReadLine());
            Console.WriteLine(String.Format("Here is the entered data\n Name of chocolate:\t{0}\t{1}\t{2} \n Weight of chocolate:\t{3} g\t{4} g\t{5} g\n Price of chocolate:{6} $\t{7} $\t{8} $", chocolate1, chocolate2, chocolate3, weight1, weight2, weight3, price1, price2, price3));
            if (((weight1 > weight2) && (weight1 > weight3)))  
            {
             
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate1,weight1,price1));
           
            }

            else if (((weight2 > weight1) && (weight2 > weight3)))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate2, weight2, price2));
            }
            else if (((weight3 > weight1) && (weight3 > weight2)))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate3, weight3, price3));
            }
            else if ((price1<price2) && (price1<price3))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate1, weight1, price1));
            }

            else if ((price2 < price1) && (price2 < price3))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate2, weight2, price2));
            }
            else if ((price3 < price1) && (price3 < price2))
            {
                Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate3, weight3, price3));
            }

        }
    }
}
我的代码正在运行,但是如果有人知道更好的方法,请您能写给我吗?
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,535
地点
弗吉尼亚州切萨皮克
编程经验
10+
您的解决方案不一定会占用过多的内存,但是会出现很多重复的行。

当您看到自己正在复制和粘贴代码时,通常表明您需要使用方法来删除重复项。同样,当您使用带数字后缀的名称时,也应该使用某种数据结构-它可以像数组一样简单,或者更复杂。在这种情况下,您实际上并不需要它。

无论如何,我们不会为您编写代码,但是请考虑以下替代伪代码:
C#:
class Chocolate
{
    string Name;
    decimal Price;
    decimal Mass;
    decimal PricePerGram => Price / Mass;
}

class Program
{
    Chocolate InputChocolate()
    {
        chocolate = new Chocolate();
        input chocolate.Name from user
        input chocolate.Price from user
        input chocolate.Mass from user
        return chocolate
    }

    void Main()
    {
        bestChocolate = InputChocolate();
        do the following 2 times
        {
            chocolate = InputChocolate();

            if chocolate.PricePerGram is less than bestChocolate.PricePerGram
                bestChocolate = chocolate;
        }

        Output $"The best chocolate is {bestChocolate.Name} at {bestChocolate.PricePerGram} dollars per gram.";
    }
}
 
Last edited:

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,535
地点
弗吉尼亚州切萨皮克
编程经验
10+
And if you are into using LINQ, Main() could potentially look something like this:
C#:
var best = Enumerable.Repeat(3, 0)
                     .Select(_ => InputChocolate())
                     .Min(c => c.PricePerGram);
Console.WriteLine($"The best chocolate is {best.Name} at {best.PricePerGram} dollars per gram.");
 
最佳 底部