如何泡泡排序存储一个集体字段的数组?

发布于 2025-01-29 20:35:55 字数 2113 浏览 1 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文