c# - 试图从一个类中创建一个对象列表,但它似乎不起作用
问题描述
所以我正在尝试创建对象列表,其数量由用户输入确定。然后我通过一个循环来收集每个对象所需的数据。我尝试过使用和不使用构造函数,以及几种不同的变体,但是每当我尝试打印结果时,什么都不会打印。就好像它正在创建空对象,所以当我运行一个 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
}
}
解决方案
要看的地方是:
Console.WriteLine("Are these parameters correct? Y/N");
}
Console.WriteLine();
// create object and assign values
要解决您的主要问题,您必须卸下支架。
Console.WriteLine("Are these parameters correct? Y/N");
Console.WriteLine();
// create object and assign values
如果您询问“这些参数是否正确?是/否”,您还应该等待输入并处理它。埃里克
推荐阅读
- php - Google chrome 在我的 yii2 应用程序中从 https 重定向到 http
- linux - 将文件夹添加到监视目录时不会触发 inotifywait close_write
- python - 失败的前提条件:Python 解释器状态未初始化。该过程可能会终止
- java - 如何在生产服务器中将 LocalTime 转换为 UTC 时间?
- php - 在 Laravel 中安装时出现 joedixon/laravel 翻译错误:不支持的操作数类型
- visual-studio-2019 - 使用 Visual Studio 的 Excel Web 加载项自定义函数
- xamarin - 用户在应用程序/设备锁定/解锁之间切换时不会触发 OnAppearing
- android - Gson 错误地将 true 解析为 false
- php - 是否可以在不重新上传的情况下旋转 [并保存] 已上传的图像?
- kotlin - MockK 无法模拟在其类之外定义的函数