C#Console应用程序使用方法编译错误(初学者寻求帮助)

时间:2016-09-30 03:12:57

标签: c# methods compiler-errors console-application

所以我对编程非常陌生,我试图从我的书中编写一个程序。

这是作业:

编写一个程序,让这个学校俱乐部能够确定他们的直板销售的一些事情。他们可以输入诸如他们有多少箱子,每箱的成本,每个糖果棒的成本等等。然后程序使用方法将返回一些计算,如他们的毛利润,扣除%由于他们的学生政府,并给予净利润。

  

错误CS0116命名空间不能直接包含诸如字段或方法Assignment3Israel之类的成员   错误:CS0103名称' totlalDuesOwedIn'在当前背景下不存在Assignment3Israel
  错误:CS0029无法隐式转换类型'字符串'到十进制' Assignment3Israel

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

namespace Assignment3Israel
{
    class GranolaBarSalesProject
    {
        //Constant variable sga 10% cut per candy bar sold//
        const decimal sgaDues = .010m;

        static void Main(string[] args)
        {
            //Capture total number of candy bars//
            decimal totalAmountCases = GetCases();
            //Capture cost per case of candy bars//
            decimal perCaseCost = GetCaseCost();
            //Capture total cost of goods (cogs)
            decimal cogs = CalctotalAmountCases(perCaseCost);
            //Capture total number of candy bars per case
            int candyBarsPerCase = GetCandyBarsPerCase();
            //Get cost per candy bar
            decimal pricePerCandyBar = GetPricePerCandyBar();
            //Get the total amount of candy bars
            decimal totalAmountCandyBars = CalctotalAmountCases(candyBarsPerCase);
            //Get the amount of candy bars sold
            decimal totalCandyBarsSold = GetTotalCandyBarsSold();
            //Calculate the total amount of bars sold 
            decimal totalCandyBarSales = CalctotalCandyBarsSold(pricePerCandyBar);
            //Calculate the Gross Profits
            decimal grossProfit = CalctotalACandyBarSales(cogs);
            //Capture the total amount due to the student government
            decimal totalDuesOwed = CalcgrossProfit(sgaDues);
            decimal netProfit = CalcgrossProfit(totalDuesOwed);
            Console.ReadKey();
        }

        public static string GetCases()
        {
            Console.Write("How many cases of candy bars: ");
            return Console.ReadLine();
        }

        public static string GetCaseCost()
        {
            Console.Write("How much per case of candy bars: ");
            return Console.ReadLine();
        }

        public static string GetCandyBarsPerCase()
        {
            Console.Write("how many candy bars per case: ");
            return Console.ReadLine();
        }

        public static string GetPricePerCandyBar()
        {
            Console.Write("how much per candy bar: ");
            return Console.ReadLine();
        }

        public static string GetTotalCandyBarsSold()
        {
            Console.Write("how many candy bars did you sell: ");
            return Console.ReadLine();
        }



    }
    public static decimal CalctotalAmountCandyBars(decimal candyBarsPerCase)
    {
        return totalAmountCases * candyBarsPerCase;
        //returns the total number of candy bars
    }
    //Calculating the total nuber of candy bars and returning that calculation
    public static void ShowTotalAmountCandyBars(string totlalAmountCandyBarsIn)
    {
        Console.WriteLine("Here are the total number of candy bars {0}: ", totlalAmountCandyBarsIn);
    }


    public static decimal CalctotalCandyBarSales(decimal pricePerCandyBar)
    {
        return totalCandyBarsSold * pricePerCandyBar;
        //returns the candy bar sales before the 10% cut to Student gov
    }
    //Calculating candy bar sales and displaying amount
    public static void ShowtotalCandyBarsales(string totalCandyBarSalesIn)
    {
        Console.WriteLine("Here is the Candy Bar sales before dues to SG {0:C}: ", totalCandyBarSalesIn);
    }


    public static decimal CalcgrossProfit(decimal cogs)
    {
        return totalCandyBarSales - cogs;
        //returns the gross profit
    }
    //Calculating the gross profit and returning that calculation
    public static void ShowgrossProfit(string grossProfitIn)
    {
        Console.WriteLine("Here is the Gross Profits of sales {0:C}: ", grossProfitIn);
    }

    public static decimal CalctotalDuesOwed(decimal sgaDues)
    {
        return grossProfit * sgaDues;
        //returns the total amount due to SG
    }
    //Calculating the total amount of dues to SG and display
    public static void ShowtotalDuesOwed(string totalDuesOwedIn)
    {
        Console.WriteLine("Here is the total due to the Student Government {0:C}: ", totlalDuesOwedIn);
    }

    public static decimal CalcnetProfit(decimal totalDuesOwed)
    {
        return grossProfit - totalDuesOwed;
        //returns the net profit of candy bar sales
    }
    //Calculating the net profit and displaying it
    public static void ShownetProfit(string netProfitIn)
    {
        Console.WriteLine("Here is the Net Profit of your sales {0:C} ", netProfitIn);

    }

}

2 个答案:

答案 0 :(得分:2)

  

错误CS0116命名空间不能直接包含字段或方法等成员

您有很多在类外声明的方法,这是不允许的。将方法移动到标记类范围的括号内。

  

错误:CS0103名称' totlalDuesOwedIn'在当前上下文中不存在

方法totlalDuesOwedIn不存在。这可能是一个错字,你打算键入" totalDuesOwedIn"。

  

错误:CS0029无法隐式转换类型'字符串'到十进制'

您的方法返回string类型的值,但您尝试将值存储在decimal类型的变量中。这两种类型不能与其他类型隐式兼容,但您可以使用decimal.Parse(stringVal)之类的转换方法来尝试转换它们。

答案 1 :(得分:1)

有很多错误,但要开始

由于声明了totalAmountCases的位置

,因此无法编写
public static decimal CalctotalAmountCandyBars(decimal candyBarsPerCase)
{
    return totalAmountCases * candyBarsPerCase;
    //returns the total number of candy bars
}

将totalAmountCases声明移出main - :

class GranolaBarSalesProject
{
    const decimal sgaDues = .010m;
    decimal totalAmountCases;

    static void Main(string[] args)
    {
        //Capture total number of candy bars//
        totalAmountCases = GetCases();

    }