2016-01-10 2 views
-4

Есть моя программа.Форматирование простого консольного вывода

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

хорошо, Джеймс, как насчет «объяснения» проблемы? хорошо, здорово, что у вас есть программа, но что вы хотите, чтобы SO-пользователи сделали с ней? вы сталкиваетесь с какой-то «проблемой», если да, пожалуйста, объясните. – Ravish

ответ

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."); 
+0

Ах. Я понимаю! {0} говорит ему ссылаться на переменную. –

+0

{0} сообщает ему ссылку на переменную _first_. Вы также можете сделать 'Console.WriteLine (« Вы были вычтены $ {0} за {1}. », Сумма, причина);' – Ian

+0

@JamesPurdon Пожалуйста, прочитайте [Что делать, если кто-то отвечает на мой вопрос?] (Http: //stackoverflow.com/help/someone-answers). – Ian

0

Если я правильно понял вашу проблему, вам просто нужно будет заменить $ на {0}, чтобы показать фактические значения.

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); 
Смежные вопросы