2016-03-04 55 views
0

我正在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;     
     } 
    }    

回答

0

您(使用)的静态方法不能使用的具体类。

0

对于这样简单的事情,您可以在单个静态方法中完成所有工作。除非,您正在专门尝试使用方法返回值等。

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

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

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 
}