简体   繁体   English

C#中的简单Windows应用程序混乱

[英]Simple Windows Application program in C# confusion

My program is messed up bad, and I need help! 我的程序搞砸了,我需要帮助! I already tried c++, and c# is new for me. 我已经尝试过c ++,而c#对我来说是新的。 Experts here at the university said that c# is like c++, and so I tried to widen my perspective on other programming languages other that c++. 该大学的专家说c#就像c ++,所以我试图扩大对c ++以外的其他编程语言的了解。 I tried to make a program that computes the sum of the lowest 3 numbers in 5 inputted numbers on windows application. 我试图制作一个程序,计算Windows应用程序上5个输入数字中的最低3个数字的总和。

to view the design of the windows application is here: Design View 在此处查看 Windows应用程序的设计: 设计视图

and my messed up code: 和我弄乱的代码:

namespace Array_excercise
{
   public partial class Form1 : Form
   {
    public Form1()
    {
        InitializeComponent();
    }
    int[] nums = { 0, 0, 0, 0, 0 };
    int[] lownum = { 0, 0, 0,0,0 };
    int[] highnum = { 0, 0, 0, 0, 0 };
    private void computbtn_Click(object sender, EventArgs e)
    {
        nums[0] = Int32.Parse(textBox1.Text);
        nums[1] = Int32.Parse(textBox2.Text);
        nums[2] = Int32.Parse(textBox3.Text);
        nums[3] = Int32.Parse(textBox4.Text);
        nums[4] = Int32.Parse(textBox5.Text);
        int result;
        for (int a = 0; a <= 4; a++)
        {
            if (nums[a] < nums[0])
                lownum[a] = nums[a];
            else if (nums[a] < nums[1])
                lownum[a] = nums[a];
            else if (nums[a] < nums[2])
                lownum[a] = nums[a];
            else if (nums[a] < nums[3])
                lownum[a] = nums[a];
            else if (nums[a] < nums[4])
                lownum[a] = nums[a];
            else
                highnum[a] = nums[a];
        }
    }
}

After that, I don't know how to compute the sum. 在那之后,我不知道如何计算总和。 for now, I'm particularly learning how to use arrays with if and for loop functions. 现在,我特别在学习如何将数组与if和for循环函数一起使用。 I'll be very grateful if those functions are used in this program. 如果程序中使用了这些功能,我将不胜感激。

I thank you in advance! 我提前谢谢你!

使用LINQ

var lowSum = nums.OrderBy(n => n).Take(3).Sum();

A one-dimensional array can be sorted with the static Sort() method. 一维数组可以使用静态Sort()方法进行排序。

https://msdn.microsoft.com/en-us/library/system.array.sort(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.array.sort(v=vs.110).aspx

So in your example, it would go something like: 因此,在您的示例中,它将类似于:

// populate the array
nums[0] = Int32.Parse(textBox1.Text);
nums[1] = Int32.Parse(textBox2.Text);
nums[2] = Int32.Parse(textBox3.Text);
nums[3] = Int32.Parse(textBox4.Text);
nums[4] = Int32.Parse(textBox5.Text);

// sort the array from lowest to highest
Array.Sort(nums);

// declare a variable to hold the sum
int sum = 0;

// iterate over the first (smallest) three
for(int i=0;i<3; i++)
{
   sum += nums[i];
}

Console.WriteLine("The sum of the three smallest is: " + sum);

Try with some sorting algorithms, for example bubble sort to sort your array: 尝试使用一些排序算法,例如冒泡排序对数组进行排序:

int c = -1;
for (int i = array.Length; i > 1; i--)
{
    for (int j = 0; j < i - 1; j++)
    {
        c = j;   
        if (array[j] < array[j + 1])
        {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
        }
    }   
}

After you have the sorted array, you can get the sum of the first 3 lowest number: 在对数组进行排序后,您可以获得前三个最低数字的总和:

int sum = 0;
for(int i=0; i < 3; i++)
{
    sum += array[i];
}

After putting all the values into "nums". 将所有值都放入“ nums”之后。 You can set the values of the two arrays using the following code. 您可以使用以下代码设置两个数组的值。

List<int> tempnums = nums.ToList(); //Creates a List from "nums" array.

tempnums.Sort(); //Sorts the List from smallest number to highest

int[] lownums = tempnums.ToArray(); //Creates an array from "tempnums" List.

tempnums.Reverse();//Reverses the values of "tempnums" so that the numbers are highest to lowest.

int[] highnums = tempnums.ToArray();//Creates an array from "tempnums" List.

And then there are two ways to get the result. 然后有两种方法可以得到结果。

int result = 0;
for(int i = 1; i <= 3; i++)
{
    result += nums[i];
}

Or 要么

int result = nums[0]+nums[1]+nums[2];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM