C# 每次我尝试向列表中添加值时,我都会收到一个错误
我有一个问题,我们必须在哪里制作天气应用程序类型的东西?基本上,用户可以添加一个城市和一个温度。用户还可以查看已添加的cites,如果愿意,还可以删除cites。我被困在添加一个字符串到我的列表,这是在不同的类,不明白我做错了什么C# 每次我尝试向列表中添加值时,我都会收到一个错误,c#,C#,我有一个问题,我们必须在哪里制作天气应用程序类型的东西?基本上,用户可以添加一个城市和一个温度。用户还可以查看已添加的cites,如果愿意,还可以删除cites。我被困在添加一个字符串到我的列表,这是在不同的类,不明白我做错了什么 public class Program { public static void Main(string[] args) { double size; int menu; bool running =
public class Program {
public static void Main(string[] args) {
double size;
int menu;
bool running = true;
while (running) {
Console.WriteLine("1. Add a city");
Console.WriteLine("2. View cities");
Console.WriteLine("3. Remove a city");
Console.WriteLine("4. Quit");
string str = Console.ReadLine();
int.TryParse(str, out menu);
var city = new stad();
switch (menu) {
case 1:
Console.Write("The amount of cities you want to add: "); //Anga temperatur som ska anges
size = double.Parse(Console.ReadLine());
//for loop för att ange värderna
for (int i = 0; i < size; i++) {
Console.Write($"City {(i+1)} : ");
string stadString = Console.ReadLine();
city.City.Add(stadString);
Console.Write($"Temperature for city {(i+1)} : ");
double stadDouble = double.Parse(Console.ReadLine());
city.Temperature.Add(stadDouble);
}
Console.Clear();
break;
case 2:
break;
case 3:
break;
case 4:
running = false;
break;
}
}
Console.ReadLine();
}
}
class stad {
public List<string> City { get; set; }
public List<double> Temperature { get; set; }
}
公共类程序{
公共静态void Main(字符串[]args){
双倍大小;
int菜单;
bool running=true;
(跑步时){
Console.WriteLine(“1.添加城市”);
Console.WriteLine(“2.查看城市”);
控制台。写线(“3.移除城市”);
控制台。写入线(“4.退出”);
string str=Console.ReadLine();
int.TryParse(str,out菜单);
var city=新stad();
开关(菜单){
案例1:
Console.Write(“要添加的城市数量:”;//Anga temperature som ska anges
size=double.Parse(Console.ReadLine());
//对于循环för att ange värderna
对于(int i=0;i
必须先实例化列表
类,然后才能使用它。您正在构建stad
类,但是没有在任何地方创建City
和Temperature
属性
试试这个:
class stad {
public List<string> City { get; } = new List<string>();
public List<double> Temperature { get; } = new List<double>();
}
//显示它们
foreach(城市温度中的var cityInfo)
{
WriteLine($“{cityInfo.CityName},温度为{cityInfo.temperature}”);
}
//或for循环:
对于(int i=0;看,我明白了。非常感谢!我该如何写下所有城市的名称及其温度,以便观众可以查看它们?我不会创建城市和温度的列表,应该是,嗯,我会在这里写一些额外的@Khaled。感谢您的帮助,但在添加城市时,是用户添加的,而不是我手动添加的。所以不要这样做变量CityName和Temperature需要是一个列表?因为用户可以继续添加城市,如果他们愿意的话。你应该包括你得到的错误消息,以及是哪一行引起的。
public class CityWithTemperature
{
public string CityName {get; set; }
public double Temperature {get; set; }
}
public static void Main(string[] args)
{
// declare a list which could contain CityWithTemperature classes
var cityTemperatures = new List<CityWithTemperature>();
// Create the first city
var firstCity = new CityWithTemperature();
firstCity.CityName = "New York";
firstCity.Temperature = 18.4;
cityTemperatures.Add(firstCity);
// create the second city
// you could also write it shorter:
cityTemperatures.Add( new CityWithTemperature
{
CityName = "Houston",
Temperature = 10.4
});
// display them
foreach(var cityInfo in cityTemperatures)
{
Console.WriteLine($"{cityInfo.CityName} with a temperature of {cityInfo.Temperature}");
}
// or a for loop:
for(int i=0;i<cityTemperatures.Count;i++)
{
Console.WriteLine($"{cityTemperatures[i].CityName} with a temperature of {cityTemperatures[i].Temperature}");
}
}