如何使用格式化字符串打印列表中的每个项目

问题描述 投票:0回答:2

我知道如何使用 foreach 循环打印出列表中的每个单独项目。然而,在我的代码中,它每次只打印出第一个值,而不是列表中的下一个值。

通常您创建一个列表,然后遍历列表中的每个值并单独打印出来。

不知道是不是因为我有一个方法为我要输出的列表中的每一项计算一个值,并在第一个值处停止?

这是我的代码:

支付类别和属性

 public class Payment
    {
        // total amount in GBP earned by the employee during an unspecified period (can be monthly salary, quarterly bonus, weekly laundry benefit etc.)
        public decimal TotalAmount { get; set; }

        // number of days employee was working from the UK during the period the payment is for
        public int UkWorkdayCount { get; set; }

        // number of days employee was working from a country other than the UK during period the payment is for
        public int ForeignWorkdayCount { get; set; }
    }

计算方法

public static decimal CalculateUkTaxableAmount(List<Payment> payments)
    {
        decimal totalTaxableAmount = 0;

        foreach (Payment payment in payments)
        {
            
                var totalAmount = payment.TotalAmount;
                var ukWorkdayCount = payment.UkWorkdayCount;
                var foreignWorkdayCount = payment.ForeignWorkdayCount;

                //Convert workday count to decimal type for calculations
                decimal ukWorkdayDecimal = ukWorkdayCount;
                decimal foreignWorkdayDecimal = foreignWorkdayCount;

                totalTaxableAmount = totalAmount * (ukWorkdayDecimal / (ukWorkdayDecimal + foreignWorkdayDecimal));

                return totalTaxableAmount;  
        }

        return totalTaxableAmount;
    }

主要方法

public static void Main()
    {
        var payments = new List<Payment>
        {
            new Payment { TotalAmount = 3000m, UkWorkdayCount = 15, ForeignWorkdayCount = 5 }, //Total UK taxable amount is: 2250.00 GBP
            new Payment { TotalAmount = 1281.50m, UkWorkdayCount = 3, ForeignWorkdayCount = 18 }, //Total UK taxable amount is: 183.07 GBP
            new Payment { TotalAmount = 250m, UkWorkdayCount = 31, ForeignWorkdayCount = 0 }, //Total UK taxable amount is: 250.00 GBP
            new Payment { TotalAmount = 50.63m, UkWorkdayCount = 0, ForeignWorkdayCount = 4 }, //Total UK taxable amount is: 0.00 GBP

        };

        //Validation for each payment
        foreach(var p in payments)
        {
            Validate(p);
        }

        var totalTaxableAmount = CalculateUkTaxableAmount(payments);

        //Write each individual item in the list to the console
        payments.ForEach(payments =>
        Console.WriteLine(string.Format("Total UK taxable amount is: {0} GBP",
            totalTaxableAmount.ToString("F", CultureInfo.InvariantCulture))));


    }

这是我用来将每个项目写入控制台的方法

payments.ForEach(payments =>
        Console.WriteLine(string.Format("Total UK taxable amount is: {0} GBP",
            totalTaxableAmount.ToString("F", CultureInfo.InvariantCulture))));

为了检查每个列表项是否正确,我运行了以下命令来打印出每个值,它按预期打印列表中的每个值

foreach (Payment p in payments)
        {
            Console.WriteLine(p.TotalAmount);
        }

预期结果是:

  • 英国应纳税总额为:2250.00 英镑
  • 英国应纳税总额为:183.07 英镑
  • 英国应纳税总额为:250.00 英镑
  • 英国应纳税总额为:0.00 英镑

但我得到:

  • 英国应纳税总额为:2250.00 英镑
  • 英国应纳税总额为:2250.00 英镑
  • 英国应纳税总额为:2250.00 英镑
  • 英国应纳税总额为:2250.00 英镑

我还创建了一个新列表以在 CalculateUkTaxableAmount() 方法之后添加每个值并对其进行迭代,但输出再次与上面相同

c# string loops format console-application
2个回答
1
投票

与其将整个

List<Payment>
发送到您的计算方法,不如传递每个单独的
Payment
并从那里获取值。您可以参考工作示例here

using System;
using System.Collections.Generic;
using System.Globalization;
                    
public class Program
{
    public static void Main()
    {
        var payments = new List<Payment>
        {
            new Payment { TotalAmount = 3000m, UkWorkdayCount = 15, ForeignWorkdayCount = 5 }, //Total UK taxable amount is: 2250.00 GBP
            new Payment { TotalAmount = 1281.50m, UkWorkdayCount = 3, ForeignWorkdayCount = 18 }, //Total UK taxable amount is: 183.07 GBP
            new Payment { TotalAmount = 250m, UkWorkdayCount = 31, ForeignWorkdayCount = 0 }, //Total UK taxable amount is: 250.00 GBP
            new Payment { TotalAmount = 50.63m, UkWorkdayCount = 0, ForeignWorkdayCount = 4 }, //Total UK taxable amount is: 0.00 GBP

        };

        //Validation for each payment
        foreach(var p in payments)
        {
            Validate(p);
        }

        //Write each individual item in the list to the console
        payments.ForEach(payments =>
        Console.WriteLine(string.Format("Total UK taxable amount is: {0} GBP",
            CalculateUkTaxableAmount(payments).ToString("F", CultureInfo.InvariantCulture))));
    }
    
    public static decimal CalculateUkTaxableAmount(Payment payment)
    {
        decimal totalTaxableAmount = 0;    
        var totalAmount = payment.TotalAmount;
        var ukWorkdayCount = payment.UkWorkdayCount;
        var foreignWorkdayCount = payment.ForeignWorkdayCount;

        //Convert workday count to decimal type for calculations
        decimal ukWorkdayDecimal = ukWorkdayCount;
        decimal foreignWorkdayDecimal = foreignWorkdayCount;  
        totalTaxableAmount = totalAmount * (ukWorkdayDecimal / (ukWorkdayDecimal + foreignWorkdayDecimal));

        return totalTaxableAmount; 

    }
}

 public class Payment
 {
     // total amount in GBP earned by the employee during an unspecified period (can be monthly salary, quarterly bonus, weekly laundry benefit etc.)
     public decimal TotalAmount { get; set; }

     // number of days employee was working from the UK during the period the payment is for
     public int UkWorkdayCount { get; set; }

     // number of days employee was working from a country other than the UK during period the payment is for
     public int ForeignWorkdayCount { get; set; }
 }

输出:

Total UK taxable amount is: 2250.00 GBP
Total UK taxable amount is: 183.07 GBP
Total UK taxable amount is: 250.00 GBP
Total UK taxable amount is: 0.00 GBP

1
投票

因为

TaxableAmount
是一个完全基于类属性的计算字段,您可以将它作为类本身的属性添加:

public class Payment
{
    public decimal TotalAmount { get; set; }
    public int UkWorkdayCount { get; set; }
    public int ForeignWorkdayCount { get; set; }

    public decimal TotalTaxableAmount
    {
        get
        {
            decimal totalWorkdays = UkWorkdayCount + ForeignWorkdayCount;
            // Avoid division by zero error by returning 0 if totalWorkdays is 0
            return totalWorkdays == 0 
                ? 0 
                : TotalAmount * (UkWorkdayCount / totalWorkdays);
        }
    }
}

然后您打印金额的代码可能看起来像这样(请注意,我将格式更改为货币并使用 GB 文化):

payments.ForEach(payment => 
    Console.WriteLine(string.Format("Total UK taxable amount is: {0} GBP", 
        payment.TotalTaxableAmount.ToString("C", new CultureInfo("en-GB")))));

输出:

Total UK taxable amount is: £2,250.00 GBP
Total UK taxable amount is: £183.07 GBP
Total UK taxable amount is: £250.00 GBP
Total UK taxable amount is: £0.00 GBP
© www.soinside.com 2019 - 2024. All rights reserved.