2013-02-24 87 views
-3

我試圖在Visual Studio中創建一個簡單的程序,將各種汽車支付添加到一起,然後計算年度成本和使用方法。在C#中創建方法,語法,括號和傳遞問題

我有一些大括號的問題,如果我正確地傳遞變量。

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 


      double loanPayment = 0; 
      double insurance = 0; 
      double gas = 0; 
      double oil = 0; 
      double tires = 0; 
      double maintenance = 0; 
      double monthlyTotal = 0; 
      double annualTotal = 0; 

      Console.WriteLine("Please enter the following expenses on a per month basis"); 
      { 
       getInput(loanPayment, insurance, gas, oil, tires, maintenance); 
       calculate(monthlyTotal, annualTotal); 
       Console.WriteLine("Your monthly total is ${0:F2} and your annual total is ${1:F2}", monthlyTotal, annualTotal); 
     } 
    }//endMain 


    static void getInput(ref double loanPayment, ref double insurance, ref double gas, ref double oil, ref double tires, ref double maintenance, ref double monthlyTotal, ref double annualTotal) 
{ 
      Console.WriteLine("How much is the loan payment?"); 
      while (!double.TryParse(Console.ReadLine(), out loanPayment)) 
      Console.WriteLine("Error, enter a number"); 

      Console.WriteLine("How much is the insurance?"); 
      while (!double.TryParse(Console.ReadLine(), out insurance)) 
       Console.WriteLine("Error, enter a number"); 

      Console.WriteLine("How much is the gas?"); 
      while (!double.TryParse(Console.ReadLine(), out gas)) 
       Console.WriteLine("Error, enter a number"); 

      Console.WriteLine("How much is the oil?"); 
      while (!double.TryParse(Console.ReadLine(), out oil)) 
       Console.WriteLine("Error, enter a number"); 

      Console.WriteLine("How much is the tires?"); 
      while (!double.TryParse(Console.ReadLine(), out tires)) 
       Console.WriteLine("Error, enter a number"); 

      Console.WriteLine("How much is the maintenence?"); 
      while (!double.TryParse(Console.ReadLine(), out maintenance)) 
       Console.WriteLine("Error, enter a number"); 
}//endgetInput 
{ 
    static void calculate(ref double loanPayment, ref double insurance, ref double gas, ref double oil, ref double tires, ref double maintenance, ref double monthlyTotal, ref double annualTotal); 
      monthlyTotal = loanPayment + insurance + gas + oil + tires + maintenance; 

      annualTotal = monthlyTotal * 12; 


     }//endCalculate 
    } 
} 
+2

無論是誰投票關閉這個真的應該停下來想一想,如果他們是有建設性的。有人在尋求幫助,這就是這個網站的用途。幫助他們。如果你不能花時間幫忙,不要來我們的網站。感謝您的理解。 – Milimetric 2013-02-25 00:57:26

回答

0

我冒昧修理你的代碼了一下。我將大多數冗餘邏輯分解了。還有更高級的東西你可以做,但我認爲這個版本很容易遵循,假設你使用它來學習。嘗試記住一個重要的概念:幹。它代表不要重複自己。待乾燥是一個很好的指引是否不是你做的很好,當你開始:

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

namespace ConsoleApplication1 
{ 
    class ProgramInputs 
    { 
     public double LoanPayment; 
     public double Insurance; 
     public double Gas; 
     public double Oil; 
     public double Tires; 
     public double Maintenance; 
    } 

    class ProgramOutputs 
    { 
     public double MonthlyTotal; 
     public double AnnualTotal; 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Please enter the following expenses on a per month basis"); 

      ProgramInputs inputs = getInputs(); 
      ProgramOutputs outputs = calculate(inputs); 

      Console.WriteLine("Your monthly total is ${0:F2} and your annual total is ${1:F2}", outputs.MonthlyTotal, outputs.AnnualTotal); 
     } 

     static ProgramInputs getInputs() 
     { 
      // make a new program input object 
      ProgramInputs inputs = new ProgramInputs(); 

      // get each input using a convenient method that factors out the parsing logic 
      inputs.LoanPayment = getSingleInput("How much is the loan payment?"); 
      inputs.Insurance = getSingleInput("How much is the insurance?"); 
      inputs.Gas = getSingleInput("How much is the gas?"); 
      inputs.Oil = getSingleInput("How much is the oil?"); 
      inputs.Tires = getSingleInput("How much are the tires?"); 
      inputs.Maintenance = getSingleInput("How much is the maintenance?"); 

      // return them in single object, it's neater this way 
      return inputs; 
     } 

     static double getSingleInput(string message) 
     { 
      double input = 0; 
      Console.WriteLine(message); 
      while (!double.TryParse(Console.ReadLine(), out input)) 
      { 
       Console.WriteLine("Error, enter a number"); 
      } 
      return input; 
     } 

     static ProgramOutputs calculate(ProgramInputs inputs) 
     { 
      ProgramOutputs outputs = new ProgramOutputs(); 
      outputs.MonthlyTotal = inputs.LoanPayment + inputs.Insurance + inputs.Gas + inputs.Oil + inputs.Tires + inputs.Maintenance; 
      outputs.AnnualTotal = outputs.MonthlyTotal * 12; 

      return outputs; 
     } 
    } 
} 
1
  • ref你沒有使用過的值沒有必要。使用out代替
  • 不要重複自己 - 提取錯誤消息不斷
  • 雙的默認值是0,沒必要和初始化0
  • (靜態)方法應該是類主體
  • 有許多方法參數是不是一個好主意,即使函數式編程風格

這裏是固定碼:

internal class Program 
{ 
    private const string ErrMsg = "Error, enter a number"; 

    private static void Main(string[] args) 
    { 
     double loanPayment, insurance, gas, oil, tires, 
       maintenance, monthlyTotal, annualTotal; 

     Console.WriteLine("Please enter the following expenses on a per month basis"); 

     GetInput(out loanPayment, "loan payment"); 
     GetInput(out insurance, "insurance"); 
     GetInput(out gas, "gas"); 
     GetInput(out oil, "oil"); 
     GetInput(out tires, "tires"); 
     GetInput(out maintenance, "maintenance"); 

     Calculate(out monthlyTotal, out annualTotal, loanPayment, insurance, gas, oil, tires, maintenance); 

     Console.WriteLine("----------------------------"); 
     Console.WriteLine("Your monthly total is ${0:F2} and your annual total is ${1:F2}", monthlyTotal, annualTotal); 
     Console.WriteLine("----------------------------"); 
     Console.ReadLine(); 
    } 

    private static void GetInput(out double value, string message) 
    { 
     Console.WriteLine("How much is the {0}?", message); 
     while (!double.TryParse(Console.ReadLine(), out value)) 
      Console.WriteLine(ErrMsg); 
    } 

    private static void Calculate(out double monthlyTotal, out double annualTotal, double loanPayment, 
            double insurance, double gas, double oil, double tires, double maintenance) 
    { 
     monthlyTotal = loanPayment + insurance + gas + oil + tires + maintenance; 
     annualTotal = monthlyTotal * 12; 
    } 
} 
0

在方法簽名之前,您有calculate方法的開頭大括號。

你已經把一個分號在calculate方法簽名結束後,身體前。

2

最好的做法是創建一個結構或類來保存你的數據並封裝你的計算函數。一個骨架可能是

public class CarLoan 
{ 
public CarLoan() 
{ 
} 

public GetInput() 
{ 
// Input 
} 

public Calculate() 
{ 
// Calculate loan 
} 
}