无法在C#中的for循环外打印我的结果

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

我对C#很新,而且这个练习一直困扰着我。基本的想法是我获得了许多Pheonix的输入,并且需要读取每个Pheonix的机身长度,宽度和长度,以便计算它们所居住的年数。我创建了一个for循环来读取所有参数,但我只是不知道如何在每个循环之后将结果发布到最后而没有输出。例如,我有一个输入:

2 phoenixes:
P1:
Body length: 100
Body width: 50
Length of 1 wing: 30
Total years: 100 ^ 2 * (50 + 2 * 30) = 1100000
P2:
Body length: 150
Body width: 25
Length of 1 wing: 10
Total years: 150 ^ 2 * (25 + 2 * 10) = 1012500

- 我应该得到输出:

2 100 50 30 150 25 10 1100000 1012500.

相反,我得到输出:

2 100 50 30 1100000 1012500 150 25 10 1100000 1012500.

我该如何避免这种情况?

int pheonixamount = int.Parse(Console.ReadLine());

for (int i = 0; i < pheonixamount; i++)
{
    List<double> pheonix = new List<double>(3);
    double bodylength = double.Parse(Console.ReadLine());
    double bodywidth = double.Parse(Console.ReadLine());
    double lengthof1wing = double.Parse(Console.ReadLine());
    pheonix.Add(bodylength);
    pheonix.Add(bodywidth);
    pheonix.Add(lengthof1wing);
    double result = Math.Pow(bodylength, 2) * (bodywidth + 2 * lengthof1wing);
    Console.WriteLine(result);
}
c# for-loop cycle
2个回答
1
投票

现在是宣布Phoenix课程的时候了:

public class Phoenix {
  public Phoenix(double bodyLength, 
                 double bodyWidth, 
                 double lengthWidth) {
    if (bodyLength < 0) 
      throw new ArgumentOutOfRangeException(nameof(bodyLength)); 
    else if (bodyWidth < 0) 
      throw new ArgumentOutOfRangeException(nameof(bodyWidth)); 
    else if (lengthWidth < 0) 
      throw new ArgumentOutOfRangeException(nameof(lengthWidth)); 

    BodyLength = bodyLength;
    BodyWidth = bodyWidth;
    LengthWidth = lengthWidth;
  } 

  public double BodyLength {get;}
  public double BodyWidth {get;}
  public double LengthWidth {get;}

  public double TotalYears {
    get {
      return BodyLength * BodyLength * (BodyWidth + 2 * LengthWidth);
    }
  }

  public override string ToString() {
    return $"{BodyLength} {BodyWidth} {LengthWidth}"; 
  }
}

现在让我们把所有的凤凰都读成一个集合(List<Phoenix>):

 int pheonixamount = int.Parse(Console.ReadLine());

 List<Phoenix> phoenixes = new List<Phoenix>();

 for (int i = 0; i < pheonixamount; i++) {
   Phoenix bird = new Phoenix(
     double.Parse(Console.ReadLine()),
     double.Parse(Console.ReadLine()), 
     double.Parse(Console.ReadLine())
   );

   phoenixes.Add(bird);
 }   

最后,让我们根据report集合制作一个phoenixes

 string report = string.Join(" ",
   // 2 - number of phoenixes
   phoenixes.Count().ToString(),                                
   // phoenixes' exteriers (lengths and widths)
   string.Join(" ", phoenixes),                                 
   // phoenixes' total years - a pinch of Linq - Select
   string.Join(" ", phoenixes.Select(item => item.TotalYears)) 
 );

 Console.Write(report); 

0
投票

而不是在同一循环中写入结果使用另一个循环来打印控制台上的所有值。根据你想要的输出你需要使用Console.Write()而不是Console.WriteLine()

您的代码在for循环中的每次迭代后创建新列表,将其从for循环中取出

int pheonixamount = int.Parse(Console.ReadLine());
List<double> pheonix = new List<double>(pheonixamount); //Use pheonixamount instead of constant value
List<double> resultList = new List<double>(pheonixamount); 
for (int i = 0; i < pheonixamount; i++)
{
    double bodylength = double.Parse(Console.ReadLine());
    double bodywidth = double.Parse(Console.ReadLine());
    double lengthof1wing = double.Parse(Console.ReadLine());

    //Best way to store three values is to create new class, instead of storing in three different variables
    pheonix.Add(bodylength);
    pheonix.Add(bodywidth);
    pheonix.Add(lengthof1wing);
    double result = Math.Pow(bodylength, 2) * (bodywidth + 2 * lengthof1wing);
    resultList(result);
}

    //Print your pheonix values
   for (int i = 0; i < pheonixamount; i +=3)
   {
        //i, i+1, i+2 will give you bodyLength, bodyWidth, lengthof1wing respectively
        Console.Write(pheonix[i] +" "+ pheonix[i+1] +" "+ pheonix[i+2]);
   }

    //Print your result
   foreach (var item in resultList)
   {
        Console.Write(item);
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.