2016-03-04 109 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 
}