C# 代码中有点混乱

C# 代码中有点混乱,c#,.net,console-application,C#,.net,Console Application,大家好,我正在读一本名为《开始Visual C#2012编程》的书。在本书第4章的“删除”部分中,他们给出了以下示例 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChapterFourExcerciseFour { class Program { stati

大家好,我正在读一本名为《开始Visual C#2012编程》的书。在本书第4章的“删除”部分中,他们给出了以下示例

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();
        }
    }
}
我不明白为什么在年份附近使用{1}意味着他们正在访问”,totalYears,totalYears==1?“”:“s”“这段代码,你为什么要访问这段代码,为什么他们不简单地写

Console.WriteLine("in {0} years you'll have the balance of {1}.",totalYears,balance);
但是,当我试图通过上述行编译代码时,编译器给出了错误:

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


为什么会这样?有人能解释一下吗?

这是一个打字错误,应该说:

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

想法是让它在1年内写
或在2年内写
等等。作者犯了一个错误,加了一个额外的“s”。

这是一个打字错误,应该说:

Console.WriteLine("in {0} year{1} you'll have the balance of {2}.",totalYears, totalYears == 1 ? "" : "s", balance);
想法是让它在1年内写
或在2年内写
等等。作者犯了一个错误,加了一个额外的“s”

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

这与英语语法有关。如果值不是
1
,则在年末追加
s
。这是因为在英语中你说的是
1年
2年
3年
等等。代码应为:

Console.WriteLine("in {0} year{1} you'll have the balance of {2}.", totalYears, totalYears == 1 ? "" : "s", balance);
由于占位符应该是递增的,并且等于
args的数量-1
,因此您将收到您声明的错误。试试这个:

Console.WriteLine("in {0} years you'll have the balance of {1}.", totalYears, balance);
我不明白为什么在年份附近使用{1}意味着他们正在访问“”,totalYears,totalYears==1?“”s“”这段代码,你为什么要访问这段代码,为什么他们不简单地写

这与英语语法有关。如果值不是
1
,他们会在年终加上
s
。这是因为在英语中你说
1年
2年
3年
,等等。代码应该是:

Console.WriteLine("in {0} year{1} you'll have the balance of {2}.", totalYears, totalYears == 1 ? "" : "s", balance);
由于占位符应该是递增的,并且等于args的数量-1,因此出现您所声明的错误。请尝试以下操作:

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

这只是为了区分一个实体和多个实体

解释:-


它用于在“年”结束时应用“s”表示如果它只有1,那么它将是“年”,否则它超过1,那么它将是“年”

这只是为了区分1个实体和多个实体

解释:-


它用于在“年”结束时应用“s”,意思是如果它只有1,那么它将是“年”,否则它将是“年”

代码
totalYears==1?”:“s”
表示如果超过或少于一年,则输入“s”字符,否则为空字符串


这就是为什么你不会得到“在1年内”这一行,它读起来不是很好。

代码
totalYears==1?”:“s”
表示如果有超过或少于一年的时间,则输入一个“s”字符,否则为空字符串

Console.WriteLine("in {0} years{1} you'll have the balance of {2}.",totalYears, totalYears == 1 ? "" : "s", balance);
这是因为你不会明白“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年”,你就不会有正确的语法

我希望这有帮助

上面这一行可能只是打字错误,在几年内应该没有“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年”,你就不会有正确的语法


我希望这有帮助!

我觉得你的替代代码很好。我看不出它为什么不能编译。你确定你在这里发布了正确的代码吗?我觉得你的替代代码很好。我看不出它为什么不能编译。你确定你在这里发布了正确的代码吗?好的,如果只是为了在年底发布,那么我也应该使用bWriteLine(“在{0}年内,您将拥有{1}的余额。”,totalYears,balance);但它不是working@psnLoverCSharp这是因为你使用了{2}。将它改为{1}就可以了。从外观上看,你甚至在发布问题时更正了错误,因为它说{1}好吧,如果它只是为了在年底放s,那么我也应该使用下面的控制台。WriteLine(“在{0}年内,你将有{1}的余额。”,totalYears,balance);但它不是working@psnLoverCSharp那是因为你使用了{2}。把它改成{1}它会很好的。从外观上看,你甚至在发布问题时纠正了错误,因为它在那里写着{1},并且会编译并像那样工作。