守则中有点混乱

时间:2013-10-22 13:16:23

标签: c# .net console-application

嗨,我正在读一本名为“Beginning Visual C#2012 Programming”的书,这本书在第4章的lopping部分中给出了以下的例子。

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);

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

  

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

为什么会这样?可以解释一下吗?

5 个答案:

答案 0 :(得分: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”。

答案 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年'时,你就没有正确的语法

我希望这有帮助!

答案 2 :(得分: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);

答案 3 :(得分:0)

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

解释: -

在“年”结束时申请“s”意味着如果它只有1那么它将是“年”,否则它将超过1它将是“年”

答案 4 :(得分:0)

代码totalYears == 1 ? "" : "s"表示如果存在多于或少于一年的字符,则为“s”字符,否则为空字符串。

这样你就不会得到“在1年内”并不能很好地阅读