嗨,我正在閱讀本書第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);
但是當我試圖通過上述行的編譯器給出了錯誤編譯代碼:
指數(從零開始)必須大於參數列表的大小大於或等於零少。
爲什麼會這樣呢?任何一個能解釋一下嗎?
您的替代代碼對我來說看起來很好。不明白爲什麼它不會編譯。你確定你已經在這裏發佈了正確的代碼嗎? – Baldrick