如何通过方法获取菜单上项目的总成本? [英] How do I pass a method to get the total cost of items on a menu?

查看:90
本文介绍了如何通过方法获取菜单上项目的总成本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用方法的新手。我需要创建一个c#程序来获取桌面订单的总成本,但似乎无法使我的方法正确。我知道它一团糟。



I am New to using methods. I need to create a c# program that get the total cost of a desk order, however cant seem to get my methods right. I know its a mess.

const int Drawer = 30;

            double totalCost = custWood + (custDrawers * DRAWER);

            //methods
            //get wood type
            //get drawer count
            //display final results

           Console.WriteLine("For a desk of type {0} and {1} drawers, the cost is {2}"); 
            Console.ReadLine();
        }//End main method

        private static decimal totalCost(string woodType, int pnumberDrawers, decimal totalCost)
        {
            decimal returnValue = 0;
            returnValue = returnValue + (30 * pNumberDrawers);
            return returnValue;

        }

        private static int GetDrawerCount()

        {
            int returnValue = 0;

            do
            {
                Console.WriteLine("How many drawers would you like?");

            } while (returnValue != 0);


        }

        private static void GetWoodType()
        {
        do
        {

        
            Console.WriteLine("What type of wood do you prefer for your desk?");
            Console.WriteLine("PINE = 100, OAK = 140, MAPLE = 180");
            string returnValue = Console.ReadLine().ToUpper();
            switch (returnValue)
            {
                case "PINE":
                    returnValue = "PINE";
                break;
                case "OAK":
                    returnValue = "OAK"; 
                break;
                case "MAPLE":
                    returnValue = "MAPLE";
                break;
                default:
                    returnValue = "bad";
                    Console.WriteLine("Whoops");
                    break;
            }
            

                } while (returnValue != "bad") ;
            
        }
    }    
}





我尝试过:



首先,我使用并行数组完成代码而不使用after方法。





然后我尝试(并且失败)使用并行阵列根据用户的选择计算出价格。

然后我尝试使用开关盒。我也尝试过简单的if语句。



What I have tried:

First I completed the code using a parallel array without using an after method.


I then tried (and failed) using a parallel array to figure out the price based in user's choice.
I then tried using a switch case. I also tried and simple if statements.

推荐答案

有太多错误;你需要先学习基础知识。从有效的应用开始;然后一次修改一下,以达到你想要的位置。您当前的方法效率不高。



Hello World - 你的第一个程序 - C#编程指南| Microsoft Docs [ ^ ]
There are so many things wrong; you need to learn the basics first. Start with an app that "works"; then modify it a bit at a time to get where you want. Your current methodology isn't very productive.

Hello World -- Your first program - C# Programming Guide | Microsoft Docs[^]


你是对的,这是一团糟。即使你的输出也会失败,很少会有效:

You are right, that is a total mess. Very little of that will work, even your output will fail:
Console.WriteLine("For a desk of type {0} and {1} drawers, the cost is {2}");

因为你没有提供格式字符串指定的任何参数值。

你有方法获取你不使用的值,方法有效在没有实际保存信息或将信息传递给外界的情况下涉及木材,... ...



我很抱歉这样说,但我不能看看那些值得保留的东西!



从头开始。

创建一个枚举 - 你知道它们是,是吗? - 适用于您的木材类型:

Because you don't provide any of the parameter values the format string specifies.
You have methods taking values you don't use, methods that work out what wood is involved without actually keeping the information or passing it to the outside world, ...

I'm sorry to say this, but I can't see anything there that is worth keeping!

Start again from scratch.
Create an enum - you know that they are, yes? - for your wood type:

public enum WoodType
    {
    Pine,
    Oak,
    Maple,
    }



现在你可以创建一个wood变量并使用它:


Now you can create a "wood" variable and use it:

WoodType selectedWood = GetWood();
Console.WriteLine("The desk is made of {0}", selectedWood);
int woodCost = GetWoodCost(selectedWood);



编写方法来选择木材并获得木材成本:


Write methods to choose the wood and get the woodcost:

public static WoodType GetWood()
    {
    ...
    }
public static int GetWoodCost(WoodType selectedWood)
    {
    switch (selectedWood)
        {
        case WoodType.Maple: return 100;
        case WoodType.Oak: return 200;
        case WoodType.Pine: return 6;
        }
    throw new ArgumentException("UNhandled wood type: " + selectedWood);
    }

(我会让你填写GetWood方法)

然后测试它,并确保它有效。

为抽屉重复类似的操作,然后进行测试。

最后,将这些位组合成一个应用程序,你就在那里。



这不难做到 - 诚实 - 但你必须准备好抛弃错误,这可能很难。你现在拥有的代码是......所有的错误。

以小块的方式做,并在你去的时候测试它们 - 你会到达那里。

(I'll let you fill in the GetWood method)
Then test it, and make sure it works.
Repeat similar operations for the drawers, and test that.
Finally, assemble those bits into an application, and you are there.

This isn't difficult to do - honest - but you have to be ready to throw away mistakes, and that can be difficult. And the code you have at the moment is ... all mistakes.
Do it in little chunks, and test them as you go - you'll get there.


花了一段时间,但这里去了。



Took a while but here goes.

int drawers;
    string wood;
    double price;

    drawers = GetDrawers();
    wood = GetWood();
    price = TotalPrice(drawers, wood);
    Display(drawers, wood, price);
    Console.ReadLine();
}

        private static void Display(int drawers, string wood, double price)
        {
            Console.WriteLine("For your desk made of {0} with {1} drawers", wood, drawers);
            Console.WriteLine("your price is {0:c}.", price);

        }

        private static double TotalPrice(int drawers, string wood)
        {
            const int PINEC = 100;
            const int OAKC = 140;
            const int MAPLEC = 180;
            const int DRAWER = 30;

            double cost = 0;
            cost = cost + drawers * DRAWER;
            if (wood == "pine")
            {
                cost = cost + PINEC;
            }
            if (wood == "oak")
            {
                cost = cost + OAKC;
            }
            if (wood == "maple")
            {
                cost = cost + MAPLEC;
            }
            return cost;
        }

        private static string GetWood()
        {
            Console.WriteLine("for desk {0}, what kind of wood?");
            Console.WriteLine("Pine, Oak, or Maple.");
            return (Console.ReadLine().ToLower());
        }

        private static int GetDrawers()
        {
            Console.WriteLine("for desk {0}, how many drawers (0-3)?");
            return Convert.ToInt32(Console.ReadLine());
        }
    }
}


这篇关于如何通过方法获取菜单上项目的总成本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆