如何在类中添加方法和方程

时间:2016-03-04 05:49:28

标签: c#

我正在使用C#进行旅行控制台应用程序,并且无法确定将方法和公式放在何处以使应用程序运行。我需要计算每加仑MPG和成本。我是编程的新手,我正在画一个空白。我附上两张初始控制台窗口应该是什么样子的图片以及结果窗口的样子。如果有人能告诉我我应该去哪个方向我会很感激。  console beginning user input window     Result Console Window

类W8M2A1_CTripAppProgram

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

    namespace W8M2A1_TripApp
    {
        class W8M2A1_CTripAppProgram
        {
            static void Main(string[] args)
            {
                //Display Welcome Message
                Console.WriteLine("Welcome to Trip Fuel Cost Calculator- Valentina       Woodson March 4, 2016 (v.1)");
                Console.WriteLine("---------------------------------------------------------------------------\n\n");

                //Prompt for destination input
                Console.Write("Enter Trip Destination City: ");
                string yourDes = Console.ReadLine();
                Console.WriteLine("\n\n");

                //Prompt for trip mileage
                Console.Write("Enter Rount Trip Mileage: ");
                double roundtMil = Convert.ToDouble(Console.ReadLine());

                Console.WriteLine("\n\n");

                //Prompt for Gallons used
                Console.Write("Enter Number of Gallons Consumed for the Trip: ");
                double galCon = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("\n\n");

                //Prompt for fuel Cost per Gallon
                Console.Write("Enter Fuel Cost Per Gallon: ");
                double fuelpGal = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("\n\n");              

                //Display information
                Console.WriteLine("Your Trip Cost Are Shown in the Following:");
                Console.WriteLine("-------------------------------------------\n");

                Console.WriteLine("\n\n");

                Console.WriteLine("Please press any key to exit");
                Console.ReadKey();
            }
        }
    }

课程旅行:

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

    namespace W8M2A1_TripApp
    {
        class Trip
        {
            // Declare data items: destination, round trip mileage, gallons consumed, full cost per gallon
            string yourDes;
            private double roundtMil;
            private double galCon;
            private double fuelpGal;
            private double mpg;                  
        }
    }            

3 个答案:

答案 0 :(得分:0)

你(正在使用)不能在静态方法中使用具体类。

答案 1 :(得分:0)

对于这个简单的东西,你可以在单个静态方法中完成所有内容。除非,你特意尝试使用方法返回值等等。

此外,当您从控制台读取值时,您不会将值写回控制台。

收到所有输入后,您可以在Console.WriteLine(galCon*roundtMil)之后执行计算并Console.WriteLine("Your Trip Cost Are Shown in the Following:");结果。

答案 2 :(得分:0)

您可以将其添加到Trip类:

public void Fill (string yourDes, double roundtMil, double galCon, double fuelpGal)
{
    //fill all those private fields you need for calcuation
}
public void CalcAndPrint ()
{
    //calcuate and print the result to the console
}