【发布时间】:2021-12-26 14:20:16
【问题描述】:
所以我正在尝试创建对象列表,其数量由用户输入决定。然后我通过一个循环来收集每个对象所需的数据。我尝试过使用和不使用构造函数,以及几种不同的变体,但是每当我尝试打印结果时,什么都不会打印。就好像它正在创建空对象,所以当我运行一个 foreach 循环时,它什么也不打印,然后在 ReadLine 上挂起,直到我按下一个键。
有什么想法吗?
编辑:将代码摘要替换为完整代码,主要是为了确保我没有实际上添加或忘记了括号。
public class TermItem
{
// declare variables
public int Id { get; set; }
public double Area { get; set; }
public float Gravity { get; set; }
public float Weight { get; set; }
public string Planet { get; set; }
public double TerminalV { get; set; }
public double TimeToV { get; set; }
}
class Program
{
static void Main(string[] args)
{
// list of code variables
int objectCount = 0;
char confirmation = 'n';
string planet;
float Cd = .1f;
float dt = .2f;
float gravity= 9.8f;
float P = 1f;
// confirmation loop for number of objects to test
while (confirmation != 'y' && confirmation != 'Y')
{
Console.WriteLine("Please enter the number of spherical objects you wish to test:");
var objectAttempt = Console.ReadLine();
while (!int.TryParse(objectAttempt, out objectCount))
{
Console.WriteLine("Please enter a valid integer.");
objectAttempt = Console.ReadLine();
}
objectCount = int.Parse(objectAttempt);
Console.WriteLine("You entered " + objectCount + " objects. Is this correct? Y/N");
var confirmationAttempt = Console.ReadLine();
while (!Char.TryParse(confirmationAttempt, out confirmation))
{
Console.WriteLine("Please enter Y/N");
confirmationAttempt = Console.ReadLine();
}
if (confirmation == 'y' || confirmation == 'Y')
{
break;
}
while (confirmation != 'y' && confirmation != 'Y')
{
if (confirmation == 'n' || confirmation == 'N')
{
confirmationAttempt = Console.ReadLine();
while (!Char.TryParse(confirmationAttempt, out confirmation))
{
Console.WriteLine("Please enter Y/N");
confirmationAttempt = Console.ReadLine();
}
}
else
{
Console.WriteLine("Please enter Y/N");
confirmationAttempt = Console.ReadLine();
while (!Char.TryParse(confirmationAttempt, out confirmation))
{
Console.WriteLine("Please enter Y/N");
confirmationAttempt = Console.ReadLine();
}
}
}
}
List<TermItem> myItems = new List<TermItem>();
// request the base parameters for each object
for (int i = 0; i < objectCount; i++)
{
// reset 'confirmation' for confirmation loop initialization
confirmation = 'n';
Console.WriteLine();
// confirmation loop for base parameters
while (confirmation != 'y' && confirmation != 'Y')
{
// request the size of the objects
if (i == 0)
{
Console.WriteLine("Please enter the frontal surface area of the 1st object in square meters:");
}
else if (i == 1)
{
Console.WriteLine("Please enter the frontal surface area of the 2nd object in square meters:");
}
else if (i == 2)
{
Console.WriteLine("Please enter the frontal surface area of the 3rd object in square meters:");
}
else
{
Console.WriteLine("Please enter the frontal surface area of the " + i+1 + "th object in square meters:");
}
var sizeAttempt = Console.ReadLine();
float size;
while (!float.TryParse(sizeAttempt, out size))
{
Console.WriteLine("Please enter a valid number.");
sizeAttempt = Console.ReadLine();
}
float area = float.Parse(sizeAttempt);
// request the mass of the objects to prevent confusion in giving weight due to difference in gravity on planets
if (i == 0)
{
Console.WriteLine("Please enter the mass of the 1st object in kilograms:");
}
else if (i == 1)
{
Console.WriteLine("Please enter the mass of the 2nd object in kilograms:");
}
else if (i == 2)
{
Console.WriteLine("Please enter the mass of the 3rd object in kilograms:");
}
else
{
Console.WriteLine("Please enter the mass of the " + i+1 + "th object in kilograms");
}
var massAttempt = Console.ReadLine();
float mass;
while (!float.TryParse(massAttempt, out mass))
{
Console.WriteLine("Please enter a valid number.");
massAttempt = Console.ReadLine();
}
mass = float.Parse(massAttempt);
// request the planet to test on
if (i == 0)
{
Console.WriteLine("Please select a planet from the list below for the 1st object:");
}
else if (i == 1)
{
Console.WriteLine("Please select planet from the list below for the 2nd object:");
}
else if (i == 2)
{
Console.WriteLine("Please select planet from the list below for the 3rd object:");
}
else
{
Console.WriteLine("Please select planet from the list below for the " + i+1 + "th object:");
}
string[] planetList = { "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
for (int index = 0; index < planetList.Length; index++)
{
Console.Write(planetList[index] + " ");
}
Console.WriteLine();
Console.WriteLine("Note: Planet spellings are case sensitive.");
planet = Console.ReadLine();
while (!planetList.Contains(planet))
{
Console.WriteLine("Please enter a valid planet from the list.");
planet = Console.ReadLine();
}
// set the planet specifc parameters for calculations
if (planet == "Earth")
{
gravity = 9.82f;
P = 1.29f;
}
else if (planet == "Mars")
{
gravity = 3.721f;
P = .02f;
}
else if (planet == "Venus")
{
gravity = 8.87f;
P = 65f;
}
else if (planet == "Jupiter")
{
gravity = 24.79f;
P = .16f;
}
else if (planet == "Saturn")
{
gravity = 10.44f;
P = .19f;
}
else if (planet == "Uranus")
{
gravity = 8.87f;
P = .42f;
}
else if (planet == "Neptune")
{
gravity = 11.15f;
P = .45f;
}
else
{
Console.WriteLine("Please enter a valid planet.");
}
Console.WriteLine();
// verify the base parameters for each object
Console.WriteLine("You entered ");
Console.WriteLine("Item: " + i + 1 + ", Area: " + area + " m^2, Weight: " + Math.Round((mass * gravity), 2) + ", on Planet: " + planet + ".");
Console.WriteLine();
Console.WriteLine("Are these parameters correct? Y/N");
var confirmationAttempt = Console.ReadLine();
while (!Char.TryParse(confirmationAttempt, out confirmation))
{
Console.WriteLine("Please enter Y/N");
confirmationAttempt = Console.ReadLine();
}
if (confirmation == 'y' || confirmation == 'Y')
{
break;
}
while (confirmation != 'y' && confirmation != 'Y')
{
if (confirmation == 'n' || confirmation == 'N')
{
confirmationAttempt = Console.ReadLine();
while (!Char.TryParse(confirmationAttempt, out confirmation))
{
Console.WriteLine("Please enter Y/N");
confirmationAttempt = Console.ReadLine();
}
}
else
{
Console.WriteLine("Please enter Y/N");
confirmationAttempt = Console.ReadLine();
while (!Char.TryParse(confirmationAttempt, out confirmation))
{
Console.WriteLine("Please enter Y/N");
confirmationAttempt = Console.ReadLine();
}
}
}
Console.WriteLine();
// create object and assign values
myItems.Add(new TermItem
{
Id = i + 1,
Area = area,
Weight = mass*gravity,
Planet = planet,
TerminalV = (Math.Sqrt(2 * mass * gravity) / (Cd * P * area)),
TimeToV = (Math.Sqrt(2 * mass * gravity) / (Cd * P * area) + area * dt) * (1 - Cd * dt)
});
}
}
// print all data
Console.WriteLine();
Console.WriteLine("Terminal Velocity Outputs for provided objects and environments");
foreach (var item in myItems)
{
Console.WriteLine("ID: " + item.Id);
Console.WriteLine("Area: " + item.Area + "m^2");
Console.WriteLine("Weight: " + Math.Round(item.Weight, 2) + "kg");
Console.WriteLine("Planet: " + item.Planet);
Console.WriteLine("Terminal Velocity: " + Math.Round(item.TerminalV, 2) + "m/s");
Console.WriteLine("Time to Reach Terminal: " + Math.Round(item.TimeToV, 2) + "s");
}
Console.Read(); // keep the console open until key press
}
}
【问题讨论】:
-
请检查您的大括号 - 在您的代码中,右大括号比左大括号多。看起来
myItems.Add()在数据输入周期之外。 -
我用完整版更新了截断的代码。如果还有问题请告诉我?