嗨,我正在阅读本书第 4 章中的“Beginning Visual C# 2012 Programming”一书,他们给出了以下示例。
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);
但是当我试图通过上面的行编译代码时,编译器给出了错误:
索引(从零开始)必须大于或等于零且小于参数列表的大小。
为什么会这样?谁能解释一下?