代码中有点混乱

本文关键字:混乱 代码 | 更新日期: 2023-09-27 18:36:33

嗨,我正在阅读一本名为"开始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();
        }
    }
}

现在在排队

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

我不明白为什么使用{1}接近年份意味着他们正在访问" ",总计年份,总计年份 == 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);

这个想法是让它说in 1 year...in 2 years...等。不过,作者犯了一个错误,并添加了额外的"s"。

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 年"时,您将没有正确的语法

我希望这有帮助!

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

这与英语语法有关。如果值未1,则在年末附加一个s。这是因为在英语中你说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);

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

解释 :-

它是在"年"末尾应用"s"的意思是,如果它只有 1,那么它将是"年",

否则它大于 1 它将是"年"

代码totalYears == 1 ? "" : "s"说,如果年份多于或少于一年,则输入"s"字符,否则为空字符串。

这样你就不会得到读起来不太好的"1 年后"这一行