我正在尝试在Visual Studio中创建一个简单的程序,以便将各种汽车付款一起添加,然后计算年度成本和使用方法。
我在使用大括号时遇到麻烦,如果我正确传递变量。
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
}
}
答案 0 :(得分:2)
最佳做法是创建一个结构或类来保存您的数据并封装您的计算函数。骨架可能是
public class CarLoan
{
public CarLoan()
{
}
public GetInput()
{
// Input
}
public Calculate()
{
// Calculate loan
}
}
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
在方法签名之前,您有calculate
方法的左括号。
你在calculate
方法签名的末尾放了一个分号,在正文之前。
答案 3 :(得分:0)
我冒昧地修改了你的代码。我考虑了大部分冗余逻辑。你可以做更高级的事情,但我认为这个版本非常容易理解,假设你正在使用它来学习。试着记住一个重要的概念:干。它代表不要重复自己。当你开始时,DRY是一个很好的指导方针,你是否表现良好:
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;
}
}
}