如何泡泡排序存储一个集体字段的数组?
void Bubblesort(City []cities, int num)
{
for(int i = 0; i < num - 1; i++)
{
for (int j = 0; j < num - 1 - i; j++)
{
if (cities[j].CityTemperature < cities[j].CityTemperature + 1)
{
var tempvariable = cities[j];
cities[j] = cities[j + 1];
cities[j + 1] = tempvariable;
}
}
}
for (int i = 0; i < num; i++)
{
Console.WriteLine(cities[i].ToString());
}
}
static void Main(string[] args)
{
Console.WriteLine("welcome to enter the name of the city and its temperature between -60 and 60 Celsius \n you will have to enter 4 elements of each :-) \n\n\n\n");
City[] cities = new City[4];
Program myProgram = new Program();
var num = cities.Length;
for (int i = 0; i < cities.Length; i++)
{
cities[i] = new City();
Console.Write("Name of City ?: ");
cities[i].CityName = Console.ReadLine();
Console.Write("what Temp ?: ");
cities[i].CityTemperature = System.Convert.ToInt32(System.Console.ReadLine());
Console.Clear();
}
myProgram.Bubblesort(cities, num);
Console.WriteLine("press a button of choice and hit enter to close the program, \n Byebye :-)");
Console.ReadKey();
}
}
///我的城市班级
私人字符串名称; 私有int温度;
public string CityName
{
get { return name; }
set { name = value; }
}
public int CityTemperature
{
get { return temperature; }
set { temperature = value; }
}
public override string ToString()
{
return "City: " + CityName + " City temperature: " + CityTemperature;
}
因此,基本上用户输入城市名称和城市 温度存储到类的场中, 我想通过温度将阵列泡泡,这样它将 使输出从最冷到最温暖的分类。 你能看到从哪里开始吗?我一直在想 弄清楚我在调用函数时是否经过错误的参数 并一直在尝试通过循环和实验来改变。
我想也许我不比较温度值 而且它做了其他事情。但是,分类永远不会发生。 感谢您花的时间来看看这个。*
void Bubblesort(City []cities, int num)
{
for(int i = 0; i < num - 1; i++)
{
for (int j = 0; j < num - 1 - i; j++)
{
if (cities[j].CityTemperature < cities[j].CityTemperature + 1)
{
var tempvariable = cities[j];
cities[j] = cities[j + 1];
cities[j + 1] = tempvariable;
}
}
}
for (int i = 0; i < num; i++)
{
Console.WriteLine(cities[i].ToString());
}
}
static void Main(string[] args)
{
Console.WriteLine("welcome to enter the name of the city and its temperature between -60 and 60 Celsius \n you will have to enter 4 elements of each :-) \n\n\n\n");
City[] cities = new City[4];
Program myProgram = new Program();
var num = cities.Length;
for (int i = 0; i < cities.Length; i++)
{
cities[i] = new City();
Console.Write("Name of City ?: ");
cities[i].CityName = Console.ReadLine();
Console.Write("what Temp ?: ");
cities[i].CityTemperature = System.Convert.ToInt32(System.Console.ReadLine());
Console.Clear();
}
myProgram.Bubblesort(cities, num);
Console.WriteLine("press a button of choice and hit enter to close the program, \n Byebye :-)");
Console.ReadKey();
}
}
/// My City class
private string name;
private int temperature;
public string CityName
{
get { return name; }
set { name = value; }
}
public int CityTemperature
{
get { return temperature; }
set { temperature = value; }
}
public override string ToString()
{
return "City: " + CityName + " City temperature: " + CityTemperature;
}
So basically user input City name and City
temperature that are stored into fields of the class,
i want to bubblesort the array by the temperature so it will
make an output sorted from coldest to warmest.
can you see where to begin with ? i been trying to
figure out if im passing wrong arguments when calling the function
and been trying to change withing the loops and experimenting.
im thinking maybe that i dont compare the temperature values
and it does something else. however the sorting is never occuring.
appreciate the time you take to look at this.*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论