-3

我正在尝试在 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
    }
}
4

4 回答 4

2

最佳实践是创建一个结构或类来保存您的数据并封装您的计算函数。骨骼可能是

public class CarLoan
{
public CarLoan()
{
}

public GetInput()
{
// Input
}

public Calculate()
{
// Calculate loan
}
}
于 2013-02-24T23:28:55.603 回答
1
  • 不需要ref您不使用传递的值。out改为使用
  • 不要重复自己 - 将错误消息提取到常量
  • double 的默认值为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;
    }
}
于 2013-02-24T23:28:25.597 回答
0

calculate在方法签名之前有方法的左大括号。

calculate您在方法签名的末尾、正文之前放置了一个分号。

于 2013-02-24T23:28:44.373 回答
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;
        }
    }
}
于 2013-02-25T00:56:09.907 回答