2013-10-22 29 views
1

嗨,我正在閱讀本書第4章中的lopping部分,名爲「開始Visual C#2012編程」,他們給出了下面的示例。在代碼中有點混淆

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ChapterFourExcerciseFour 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      double balance, interestRate, targetBalance; 
      int totalYears = 0; 

      //reading balance from the console and saving it into the balance 
      Console.WriteLine("Please Enter your balance"); 
      balance = Convert.ToDouble(Console.ReadLine()); 

      //reading interesrrate from the console and saving it into tht interesrrate 
      Console.WriteLine("What is your current interest rate"); 
      interestRate = Convert.ToDouble(Console.ReadLine()); 

      //reading targetbalance from the console and saving it int the targetbalance 
      Console.WriteLine("What balancce would you like to have"); 
      targetBalance = Convert.ToDouble(Console.ReadLine()); 

      do 
      { 
       balance *= interestRate; 
       ++totalYears; 
      } 
      while (balance < targetBalance); 
      Console.WriteLine("in {0} years{1} you'll have the balance of {2}.",totalYears, totalYears == 1 ? "" : "s", balance); 
      Console.ReadKey(); 
     } 
    } 
} 
現在

在該行

Console.WriteLine("in {0} years{1} you'll have the balance of {2}.",totalYears, totalYears == 1 ? "" : "s", balance); 

我不明白爲什麼有使用{1}臨近年意味着他們正在訪問「」,totalYears,totalYears == 1? 「」:「S」「這段代碼,爲什麼你訪問這些代碼,他們爲什麼不乾脆寫

Console.WriteLine("in {0} years you'll have the balance of {1}.",totalYears,balance); 

但是當我試圖通過上述行的編譯器給出了錯誤編譯代碼:

指數(從零開始)必須大於參數列表的大小大於或等於零少。

爲什麼會這樣呢?任何一個能解釋一下嗎?

+0

您的替代代碼對我來說看起來很好。不明白爲什麼它不會編譯。你確定你已經在這裏發佈了正確的代碼嗎? – Baldrick

回答

5

這是一個錯字,應該說:

Console.WriteLine("in {0} year{1} you'll have the balance of {2}.",totalYears, totalYears == 1 ? "" : "s", balance); 

的想法是把它說in 1 year...in 2 years...等。作者犯了一個錯誤,但並增加了一個額外的「s」。

+0

好吧,如果它只是爲了把s在年底,然後我也應該在下面的線 Console.WriteLine(「在{0}年,你將有餘額{1}。」,totalYears,餘額) ; 但它不起作用 –

+2

@psnLoverCSharp這是因爲您使用了{2}。將其更改爲{1}即可。通過它的外觀,您甚至在發佈問題時糾正了錯誤,正如它在{1}中所述的那樣,並且將會像這樣編譯和工作。 –

+0

感謝它的工作,我錯了:) –

0

我不明白爲什麼近年使用{1}意味着他們訪問「」,totalYears,totalYears == 1? 「」:「S」「這段代碼,爲什麼你訪問這些代碼,他們爲什麼不乾脆寫

這是英語語法做,如果他們被追加一個s到最後的歲月。值不1這是因爲在英語你說1 year2 years3 years等的代碼應該是:。

Console.WriteLine("in {0} year{1} you'll have the balance of {2}.", totalYears, totalYears == 1 ? "" : "s", balance); 

你得到你的國家的錯誤,因爲佔位符應是漸進的和等於number of args -1 。試試這個:

Console.WriteLine("in {0} years you'll have the balance of {1}.", totalYears, balance); 
0

它只是1個實體和1個多實體

交代區分: -

它是爲在「年」的結束將「S」表示如果只有1那麼它會是「年」,否則它會超過1它將會是「年」

0

代碼totalYears == 1 ? "" : "s"說如果有多於或少於一年的時間就放置's'字符,否則爲空字符串。

是這樣的話,你沒有得到其犯規讀得很好「1年」行

1
Console.WriteLine("in {0} years{1} you'll have the balance of {2}.",totalYears, totalYears == 1 ? "" : "s", balance); 

上面一行可能只是有一個錯字,應該不帶「s」,在幾年來寫:

Console.WriteLine("in {0} year{1} you'll have the balance of {2}.",totalYears, totalYears == 1 ? "" : "s", balance); 

至於爲什麼你的行沒有正確編譯是因爲你被稱爲{2},作爲第三項,它不存在。該列表從位置{0}開始,並且列表中只有2個項目。你可以把它寫成

Console.WriteLine("in {0} years you'll have the balance of {1}.", totalYears, balance); 

...但你不會有正確的語法時,它僅僅是「1年

我希望這有助於!