简体   繁体   中英

finding maximum & minimum number from 2 numbers in an array

I'm trying to find the maximum and minimum numbers out of 2 integers that a user has inputted. Firstly i have converted the string to int, then went to put them into an array so i can manipulate them. I think i'm getting stuck when it comes to assigning variables to an array. But i couldn't see any examples of arrays with variables assigned to them, which is probably where i'm going wrong.

    private void button1_Click(object sender, EventArgs e)
    {
       string txtbxnum1 = Int32.Parse(num1);
       string txtbxnum2 = Int32.Parse(num2);

       int[] numbers = new int[2] {0,1};
       int numbers [0] = num1;
       int numbers [1] = num2;

       int maximumNumber = Max.numbers();
       int minimumNumber = Min.numbers();
       MessageBox.Show (maximumNumber.Text);
    }

I would be glad of any help or direction.

If all you have is two numbers, you do not need an array: System.Math provides functions to find the smaller and the larger of two numbers, called Math.Max and Math.Min .

// Int32.Parse takes a string, and returns an int, not a string:
int n1 = Int32.Parse(num1);
int n2 = Int32.Parse(num2);
// Math.Min and Math.Max functions pick the min and max
int min = Math.Min(n1, n2);
int max = Math.Max(n1, n2);
// Show both numbers in a message box in one go using String.Format:
MessageBox.Show(string.Format("Min:{0} Max:{1}", min, max));

A little bit messed up syntax. Your code is not C# language valid code.

You have to do something like this:

var numbers = new int[]{0,1,567,4,-5,0,67....};

and max/min is simply like

var maximum = numbers.Max();
var minimum = numbers.Min();

You should be calling Math.Min and Math.Max both of which accept two integers as arguments.

Let me know if that is not sufficient detail.

int maximumNumber = Math.Max(numbers[0],numbers[1]);
int minimumNumber = Math.Min(numbers[0],numbers[1]);

MessageBox.Show(maximumNumber + " " is the largest and " + minimumNumber + " is the smallest");

That said you shouldn't really access the array values like that, but it'll work for a beginner.

I don't quite understand your interaction with the TextBoxes and strange parsing and then setting to a string, but assuming num1 and num2 are integers that the user entered

private void button1_Click(object sender, EventArgs e)
{
    int maximumNumber = Math.Max(num1, num2);
    int minimumNumber = Math.Min(num1, num2);

    MessageBox.Show (maximumNumber);
}

There are a few mistakes in your code.

string txtbxnum1 = Int32.Parse(num1);

Int32.Parse takes in a string and returns an int . However, you're trying to assign it to a string . It should be

int txtbxnum1 = Int32.Parse(num1);

Assigning an array like this:

int[] numbers = new int[2] {0,1};

simply creates a new array that can hold two integers and prefills them with the values 0 and 1 . This isn't what you want to do. As far as I can tell, you don't even need to use arrays here unless you're using it somewhere else in your code.

You can find the Max and Min values by using methods in the Math class.

int minimumValue = Math.Min(txtbxnum1,txtbxnum2);
int maximumValue = Math.Max(txtbxnum1,txtbxnum2);

You can find out more about the Math class on MSDN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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