1

嗨,我正在阅读本书第 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);

但是当我试图通过上面的行编译代码时,编译器给出了错误:

索引(从零开始)必须大于或等于零且小于参数列表的大小。

为什么会这样?谁能解释一下?

4

5 回答 5

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”。

于 2013-10-22T13:19:07.280 回答
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年”时,你不会有正确的语法

我希望这有帮助!

于 2013-10-22T13:33:39.833 回答
0

我不明白为什么在年份附近使用 {1} 意味着他们正在访问“”,totalYears,totalYears == 1?“”:“s”“这段代码,你为什么要访问这段代码,为什么他们不简单地写

这与英语语法有关。s如果值不是 ,他们会在年末追加1。这是因为在英语中你会说1 year, 2 years,3 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);
于 2013-10-22T13:20:22.913 回答
0

只是为了区分 1 个实体和 1 个以上的实体

解释:-

它用于在“年”末尾应用“s”意味着如果它只有 1 那么它将是“年”,否则它会超过 1 它将是“年”

于 2013-10-22T13:21:20.847 回答
0

代码totalYears == 1 ? "" : "s"说如果超过或少于一年,则放置一个“s”字符,否则为空字符串。

这样你就不会得到读起来不太好的“1年内”这句话

于 2013-10-22T13:21:40.587 回答