2016-01-10 92 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

,詹姆斯,那麼'解釋'這個問題呢?好吧,你有一個程序很棒,但是,你希望用戶如何處理它?你是否面臨一些「問題」,如果是的話,請解釋。 – 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(「你被{1}扣除$ {0}。」,金額,理由);' – 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);