How to validate user input for whether it's an

2019-01-20 11:17发布

It tells me that it can't convert int to bool. Tried TryParse but for some reason the argument list is invalid.

Code:

private void SetNumber(string n)
{
    // if user input is a number then
    if (int.Parse(n)) 
    {
        // if user input is negative
        if (h < 0)
        {
            // assign absolute version of user input
            number = Math.Abs(n); 
        }
        else 
        {
            // else assign user input
            number = n;
        }
    }
    else
    {
        number = 0; // if user input is not an int then set number to 0  
    }
}

10条回答
老娘就宠你
2楼-- · 2019-01-20 11:40
    private void SetNumber(string n)
    {
        int nVal = 0;

        if (int.TryParse(n, out nVal))
        {
            if (nVal < 0)
                number = Math.Abs(nVal);
            else
                number = nVal;
        }
        else
            number = 0;
    }
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-20 11:42
//vinojash@gmail.com
//In my knowledge i did this in simple way thanks for watch my code 
 static void Main(string[] args)
    {
        string a, b;
        int f1, f2, x, y;
        Console.WriteLine("Enter two inputs");
        a = Convert.ToString(Console.ReadLine());
        b = Console.ReadLine();
        f1 = find(a);   
        f2 = find(b);   

        if (f1 == 0 && f2 == 0)
        {
            x = Convert.ToInt32(a);
            y = Convert.ToInt32(b);
            Console.WriteLine("Two inputs r number \n so tha additon of these text box is= " + (x + y).ToString());
        }
        else
            Console.WriteLine("One or tho inputs r string \n so tha concadination of these text box is = " + (a + b));
        Console.ReadKey();

    }
        static int find(string s)
        {
        string s1 = "";
        int f;
         for (int i = 0; i < s.Length; i++)
            for (int j = 0; j <= 9; j++)
            {
                string c = j.ToString();
                if (c[0] == s[i])
                {
                    s1 += c[0];
                }
            }

        if (s==s1)
            f= 0;
        else
            f= 1;

        return f;
    }
查看更多
男人必须洒脱
4楼-- · 2019-01-20 11:46

You could try something like below using int.TryParse..

        private void SetNumber(string n)
        {
            int parsed = -1;
            if (int.TryParse(n, out parsed)) //if user input is a number then
            ...

The reason there are complaints that it cannot convert an int to a bool is because the return type of int.Parse() is an int and not a bool and in c# conditionals need to evaluate bool values.

查看更多
倾城 Initia
5楼-- · 2019-01-20 11:50

Just use this:

int i;
bool success = int.TryParse(n, out i);

if the parse was successful, success is true.

If that case i contain the number.

You probably got the out argument modifier wrong before. It has the out modifier to indicate that it is a value that gets initialized within the method called.

查看更多
Summer. ? 凉城
6楼-- · 2019-01-20 11:51

int.Parse will give you back an integer rather than a boolean.

You could use int.TryParse as you suggested.

int parsedValue;
if(int.TryParse(n, out parsedValue))
{
}
查看更多
SAY GOODBYE
7楼-- · 2019-01-20 11:51

int.Parse will convert a string to an integer. Current you have it within an if statement, so its treating the returned value of int.Parse as a bool, which its not.

Something like this will work:

private void SetNumber(string n)
{
    int num = 0;
    try{
        num = int.Parse(n);
        number = Math.Abs(num);
    }catch(Exception e){
        number = 0;
    }   
}
查看更多
登录 后发表回答