基于控制台的C#方法

时间:2016-12-12 18:36:07

标签: c#

我在运行包含同一类中的多个方法的程序时遇到困难。这是代码:

namespace E03_pt2_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int numberOfDrawers = 0;
            string deskWoodType = "o";
            double cost = 0;
            drawersMeth(out numberOfDrawers);
            woodTypeMeth(out deskWoodType);
            CalculateCostMeth(ref numberOfDrawers, ref cost, ref deskWoodType);
            OutPutCostMeth(ref deskWoodType , cost, numberOfDrawers);
        }//end main

        private static void drawersMeth(out int numberOfDrawers)
        {
            int numOfDrawers;
            Console.WriteLine("Enter the number of desk drawers");
            numOfDrawers = Convert.ToInt16(Console.ReadLine());
            numberOfDrawers = numOfDrawers;
        }//end drawersMeth

        private static string woodTypeMeth(out string deskWoodType)
        {
            Console.WriteLine("Enter the desk wood type. (ex. type mahogany, oak, or pine)");
            deskWoodType = Convert.ToString(Console.ReadLine());
            switch (deskWoodType)
            {
                case "mahogany":
                    {
                        deskWoodType = "m";
                        break;
                    }
                case "oak":
                    {
                        deskWoodType = "o";
                        break;
                    }
                case "pine":
                    {
                        deskWoodType = "p";
                        break;
                    }
                default:
                    {
                        deskWoodType = "error";
                        break;
                    }
            }
            return deskWoodType;
        }// end woodTypeMeth

        private static int CalculateCostMeth(ref string deskWoodType, ref int numberOfDrawers, out int cost)
        {
            int pine = 100;
            int oak = 140;
            int other = 180;
            int surchage = 30;
            if (deskWoodType == "p")
                cost = pine + (numberOfDrawers * surchage);
            else if (deskWoodType == "o")
                cost = oak + (numberOfDrawers * surchage);
            else
                cost = other + (numberOfDrawers * surchage);
            return cost;
        }// end CalculateCostMeth

        private static void OutPutCostMeth(int numberOfDrawers, string deskWoodType, int cost)
        {
            double totalCost = cost;
            Console.WriteLine("The number of drawers is {0}", numberOfDrawers);
            Console.WriteLine("The wood finish you have selected is ", deskWoodType);
            Console.WriteLine("The total cost is {0}", totalCost);
        }//end outputCost
    }//end class
}//end nameSpace

该程序的基本功能是从用户获得抽屉数量。然后,获取木材类型,然后收集并显示总成本和订单描述。

2 个答案:

答案 0 :(得分:0)

正如有几个人在评论中指出的那样,不加区分地使用outref是一种不好的做法。

话虽如此,你的第一个主要问题是编写的代码无法编译,因为你以错误的顺序传递参数。例如:

CalculateCostMeth(ref numberOfDrawers, ref cost, ref deskWoodType);

实际应该是

CalculateCostMeth(ref deskWoodType, ref numberOfDrawers, ref cost);

即便如此,它也不会编译,因为“cost”是a)错误的类型而b)应该以“out”而不是“ref”传递。

同样的事情OutPutCostMeth - 订单完全混乱。

OutPutCostMeth(ref deskWoodType, cost, numberOfDrawers);

它应该是

OutPutCostMeth(numberOfDrawers, ref deskWoodType, cost);

此外,“deskWoodType”不应​​包含“ref”关键字。

此外,“cost”是main方法中的double和“OutPutCostMeth”中的int - 您不能隐式地将double转换为int,因为这可能会导致数据丢失。以下是正确的签名:

// New signature - change int cost to double cost and change the return type
private static double CalculateCostMeth(ref string deskWoodType, ref int numberOfDrawers, out double cost)

// Change cost to a double
private static void OutPutCostMeth(int numberOfDrawers, string deskWoodType, double cost)

以下是正确的电话:

CalculateCostMeth(ref deskWoodType, ref numberOfDrawers, out cost);
OutPutCostMeth(numberOfDrawers, deskWoodType, cost);

最后一点:确保验证用户输入。现在输入一些随机字符串会使程序崩溃。我还声称我想要-20抽屉,程序告诉我这家商店欠我500美元。我强烈建议您确保处理明显无效的输入。

答案 1 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace E03_pt2_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int numberOfDrawers = 0;
            string deskWoodType = "o";
            double cost = 0;
            drawersMeth(out numberOfDrawers);
            woodTypeMeth(out deskWoodType);
            CalculateCostMeth(ref deskWoodType, ref numberOfDrawers, out cost);
            OutPutCostMeth(numberOfDrawers, deskWoodType, cost);
        }//end main
        private static void drawersMeth(out int numberOfDrawers)
        {
            int numOfDrawers;
            Console.WriteLine("Enter the number of desk drawers");
            numOfDrawers = Convert.ToInt16(Console.ReadLine());
            numberOfDrawers = numOfDrawers;
        }//end drawersMeth
        private static string woodTypeMeth(out string deskWoodType)
        {
            Console.WriteLine("Enter the desk wood type. (ex. type mahogany, oak, or pine)");
            deskWoodType = Convert.ToString(Console.ReadLine());
            switch (deskWoodType)
            {
                case "mahogany":
                    {
                        deskWoodType = "m";
                        break;
                    }
                case "oak":
                    {
                        deskWoodType = "o";
                        break;
                    }
                case "pine":
                    {
                        deskWoodType = "p";
                        break;
                    }
                default:
                    {
                        deskWoodType = "error";
                        break;
                    }
            }
            return deskWoodType;
        }// end woodTypeMeth
        private static double CalculateCostMeth(ref string deskWoodType,ref int numberOfDrawers, out double cost)
        {
            double pine = 100;
            double oak = 140;
            double other = 180;
            double surchage = 30;
            if (deskWoodType == "p")
                cost = pine + (numberOfDrawers * surchage);
            else if (deskWoodType == "o")
                cost = oak + (numberOfDrawers * surchage);
            else
                cost = other + (numberOfDrawers * surchage);
            return cost;
        }// end CalculateCostMeth
        private static void OutPutCostMeth(int numberOfDrawers, string deskWoodType, double cost)
        {
            double totalCost = cost;
            Console.WriteLine("The number of drawers is {0}", numberOfDrawers);
            Console.WriteLine("The wood type you have selected is ", deskWoodType);
            Console.WriteLine("The total cost is {0:c2}", totalCost);
        }//end outputCost
    }//end class
}//end nameSpace

这是基于您的建议的更正版本