我是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);
}
答案 0 :(得分: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
答案 1 :(得分: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);
}
}