简单控制台输出格式

时间:2016-01-10 18:17:55

标签: c#

有我的节目。

    static void Main(string[] args)
    {
        double weeklySales = 0, grossPay = 0, fedTax = 0, socSecurity = 0, retirement = 0, totDeductions = 0, takeHomePay = 0;

        Console.WriteLine("Please enter your total for sales for the week.");
        weeklySales = Convert.ToDouble(Console.ReadLine());
        grossPay = weeklySales * .07;
        fedTax = grossPay * .18;
        socSecurity = grossPay * .06;
        retirement = grossPay * .1;
        totDeductions = fedTax + socSecurity + retirement;
        takeHomePay = grossPay - totDeductions;

        Console.WriteLine("Your total sales for the week were $ ", weeklySales);
        Console.WriteLine("Your gross pay for the week was $ ", grossPay);
        Console.WriteLine("Your Federal Taxes for the week were $ ", fedTax);
        Console.WriteLine("You were deducted $ ", socSecurity, " for social security.");
        Console.WriteLine("Your retirement contribution was $ ", retirement);
        Console.WriteLine("The total amount of of deductions were $ ", totDeductions);
        Console.WriteLine("Your take home pay for the week is $ ", takeHomePay);

        Console.ReadLine();
    }

问题是输出不正确。

Please enter your total for sales for the week.
 123
 Your total sales for the week were $
 Your gross pay for the week was $
 Your Federal Taxes for the week were $
 You were deducted $
 Your retirement contribution was $
 The total amount of of deductions were $
 Your take home pay for the week is $

我需要将计算值包含在输出中。 我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,您只需将$替换为{0}即可显示实际值。

答案 1 :(得分:0)

在当前代码中,您没有指定应在字符串输出中输入值的位置。您可以按照以下方式更改它们并接收您正在寻找的输出:

        Console.WriteLine("Your total sales for the week were ${0}", weeklySales);
        Console.WriteLine("Your gross pay for the week was ${0}", grossPay);
        Console.WriteLine("Your Federal Taxes for the week were ${0}", fedTax);
        Console.WriteLine("You were deducted ${0} for social security.", socSecurity);
        Console.WriteLine("Your retirement contribution was ${0}", retirement);
        Console.WriteLine("The total amount of of deductions were ${0}", totDeductions);
        Console.WriteLine("Your take home pay for the week is ${0}", takeHomePay);

答案 2 :(得分:0)

此外,此行不起作用:

Console.WriteLine("You were deducted ${0}", socSecurity, " for social security.");

需要:

Console.WriteLine("You were deducted ${0} for social security.", socSecurity);

另一种选择是使用字符串连接:

Console.WriteLine("You were deducted $" + socSecurity + " for social security.");