c#如何优化一个简单的和?

时间:2021-06-23 21:35:57

I'm trying to add to numbers the user input in a textBox. The operation is rather simple, but I'm asked to optimize the code the best it can. So far I have created a method where I validate if the textBox is empty or not. Then I have a method for converting the Strings from the textBox into "int". However, now I don't know how to obtain those numbers and use them in my "sum" method. Perhaps I'm forgetting something basic.

我试图添加一个文本框中的用户输入。操作相当简单,但我被要求尽可能优化代码。到目前为止,我已经创建了一个方法,用于验证文本框是否为空。然后我有一个方法将文本框中的字符串转换为“int”。然而,现在我不知道如何获得这些数字并在我的“sum”方法中使用它们。也许我忘记了一些基本的东西。

Here's my code:

这是我的代码:

private void btnSum_Click(object sender, RoutedEventArgs e)
{
    String num1 = txtNum1.Text;
    String num2 = txtNum2.Text;    

    if(validate(num1,num2) == false)
    {
        MessageBox.Show("Empty fields");
    }
    else 
    {
       convertNum(num1, num2);    
       MessageBox.Show("The sum is: ");   
    }
}


public static Boolean validate(String n1, String n2) 
{
    if (n1 == null || n1.Equals("") || n2 == null || n2.Equals(""))
    {
        return false;
    }
    else 
    {
        return true;
    }
}

public static void convertNum(String n1,String n2) 
{
    int num1 = 0;
    int num2 = 0;
    try 
    {
        num1 = Int32.Parse(n1);
        num2 = Int32.Parse(n2);       
    }catch(FormatException)
    {
        MessageBox.Show("Input only numbers.");   
    }                
}

public static int sum(int n1, int n2) 
{
    int sum = n1 + n2;
    return sum;    
}

3 个解决方案

#1


3  

Seems like it would be much simpiler to just do all the logic in your button click method. All your validation really needs to do is check if the text values can be parsed into integers. int.TryParse will return false for null, empty string, and a string that is not a valid integer, so that's all you really need.

看起来,只要按一下按钮的方法就行了。您真正需要做的是检查文本值是否可以解析为整数。tryparse将返回null、空字符串和一个不是有效整数的字符串的false,因此这就是您真正需要的。

private void btnSum_Click(object sender, RoutedEventArgs e)
{
    int n1, n2;
    if(int.TryParse(txtNum1.Text, out n1) && int.TryParse(txtNum2.Text, out n2))
    {
       MessageBox.Show("The sum is: " + (n1 + n2));
    }
    else 
    {      
        MessageBox.Show("Enter valid numbers");
    }
}

#2


3  

For starters, you could change your "validate" function from this:

对于初学者,您可以从以下内容更改“validate”函数:

public static Boolean validate(String n1, String n2) 
{
    if (n1 == null || n1.Equals("") || n2 == null || n2.Equals(""))
    {
        return false;
    }
    else 
    {
        return true;
    }
}

...to this:

…:

public static Boolean validate(String n1, String n2) 
{
    return ((!string.IsNullOrEmpty(n1)) && (!string.IsNullOrEmpty(n2)));
}

...and this line:

…和这条线:

if(validate(num1,num2) == false)

...could be:

…可能是:

if (!validate(num1,num2))

#3


1  

private void btnSum_Click(object sender, RoutedEventArgs e)
{
String num1 = txtNum1.Text;
String num2 = txtNum2.Text;    

if(validate(num1,num2) == false)
{
    MessageBox.Show("Empty fields");
}
else 
{
  var result  =  convertNum(num1, num2);    
   MessageBox.Show("The sum is: "+result);   
}
}

public static int convertNum(String n1,String n2) 
{
int num1 = 0;
int num2 = 0;    
int result = 0;
try 
{
    num1 = Int32.Parse(n1);
    num2 = Int32.Parse(n2);  
    result = sum(num1,num2); 
    return result;
}
catch(FormatException)
{
    MessageBox.Show("Input only numbers.");   
    return result; 
}     

}

public static int sum(int n1, int n2) 
{
int sum = n1 + n2;
return sum;    
}

#1


3  

Seems like it would be much simpiler to just do all the logic in your button click method. All your validation really needs to do is check if the text values can be parsed into integers. int.TryParse will return false for null, empty string, and a string that is not a valid integer, so that's all you really need.

看起来,只要按一下按钮的方法就行了。您真正需要做的是检查文本值是否可以解析为整数。tryparse将返回null、空字符串和一个不是有效整数的字符串的false,因此这就是您真正需要的。

private void btnSum_Click(object sender, RoutedEventArgs e)
{
    int n1, n2;
    if(int.TryParse(txtNum1.Text, out n1) && int.TryParse(txtNum2.Text, out n2))
    {
       MessageBox.Show("The sum is: " + (n1 + n2));
    }
    else 
    {      
        MessageBox.Show("Enter valid numbers");
    }
}

#2


3  

For starters, you could change your "validate" function from this:

对于初学者,您可以从以下内容更改“validate”函数:

public static Boolean validate(String n1, String n2) 
{
    if (n1 == null || n1.Equals("") || n2 == null || n2.Equals(""))
    {
        return false;
    }
    else 
    {
        return true;
    }
}

...to this:

…:

public static Boolean validate(String n1, String n2) 
{
    return ((!string.IsNullOrEmpty(n1)) && (!string.IsNullOrEmpty(n2)));
}

...and this line:

…和这条线:

if(validate(num1,num2) == false)

...could be:

…可能是:

if (!validate(num1,num2))

#3


1  

private void btnSum_Click(object sender, RoutedEventArgs e)
{
String num1 = txtNum1.Text;
String num2 = txtNum2.Text;    

if(validate(num1,num2) == false)
{
    MessageBox.Show("Empty fields");
}
else 
{
  var result  =  convertNum(num1, num2);    
   MessageBox.Show("The sum is: "+result);   
}
}

public static int convertNum(String n1,String n2) 
{
int num1 = 0;
int num2 = 0;    
int result = 0;
try 
{
    num1 = Int32.Parse(n1);
    num2 = Int32.Parse(n2);  
    result = sum(num1,num2); 
    return result;
}
catch(FormatException)
{
    MessageBox.Show("Input only numbers.");   
    return result; 
}     

}

public static int sum(int n1, int n2) 
{
int sum = n1 + n2;
return sum;    
}