【问题标题】:C# Methods in console Based [closed]基于控制台的 C# 方法[关闭]
【发布时间】:2016-12-12 18:36:07
【问题描述】:

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

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

程序的基本功能是从用户那里获得抽屉的数量。然后,获取木材类型,然后收集并显示总成本以及订单说明。

【问题讨论】:

  • 你的代码是一堵文字墙 :(
  • 编辑您的问题。选择所有代码,然后按 Ctrl-K。然后阅读How to Ask 以及如何创建minimal reproducible example
  • 你遇到了什么困难?很难从中看出你在问什么。
  • 是的,代码有什么问题?会发生什么,您预计会发生什么?
  • 你的公式不应该是surchage + (numberOfDrawers * type)吗?此外,您的大多数方法都不需要refout,一般情况下应尽量避免使用它们。

标签: c#


【解决方案1】:

正如一些人在 cmets 中指出的那样,不分青红皂白地使用 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 美元。我强烈建议您确保像这样优雅地处理明显无效的输入。

【讨论】:

  • 感谢您的帮助。我也无法让它在“OutPutCost”中显示木材类型。
【解决方案2】:
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

这是根据您的建议修正的版本

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-08
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 2017-01-24
    相关资源
    最近更新 更多