将数字保持在一定范围内

时间:2019-12-09 21:05:26

标签: c# range

我正在尝试完成一些数据验证,但我确实为此感到挣扎。我一直在使用If语句,希望If(双关语意)有人可以帮助我。我对整个事情还很陌生,到目前为止,我已经设法完成了很多工作,但这让我很沮丧。我真的想按正确的方向推动或推动某事,因为感觉就像是我的头在一块沙纸上摩擦

        int iNumber1;
        int iNumber2;


        Console.WriteLine("Please give me 2 number between -10 and +10 and ill add them together");
        Console.Write("Please enter a number: ");
        iNumber1 = Convert.ToInt32(Console.ReadLine());
        Console.Write("Please enter a number: ");
        iNumber2 = Convert.ToInt32(Console.ReadLine());

        int iResult;
        //here i get the adding bit done
        iResult = iNumber1 + iNumber2;

        //bool bUserInput = false;



        //while (!bUserInput)
        //{
        //    string sInput = Console.ReadLine();

        //    if (iNumber1 < -10)
        //    {
        //        Console.WriteLine("Your are out of range please stay between -10 and +10");
        //    }

        //    if (iNumber1 < 10)
        //    {
        //        Console.WriteLine("Your are out of range please stay between -10 and +10 ");
        //    }

        //    if (iNumber2 < -10)
        //    {
        //        Console.WriteLine("Your are out of range please stay between -10 and +10");
        //    }

        //    if (iNumber2 < 10)
        //    {
        //        Console.WriteLine("Your are out of range please stay between -10 and +10");
        //    }

        //}


        if (iResult <= -10 || iResult >= 11)
        {
            Console.WriteLine("Out of range calculation is ignored");
        }

        else
        {
            Console.WriteLine("That number is vaild and its {0}", iResult);
        }

2 个答案:

答案 0 :(得分:0)

因此,您需要一种方法来验证数字在-10到+10(含)之间。那应该很容易:

bool isValid(int i)
{
    return (i >= -10) && (i <= 10);
}

现在您需要一个函数来查看两个数字是否均有效:

bool areValid(int i1, int i2)
{
    return isValid(i1) && isValid(i2); 
}

现在仅假设输入无效,并循环直到它是:

bool isValid = false;
while(!isValid)
{
    Console.WriteLine("Please give me 2 number between -10 and +10 and I'll add them together");
    Console.Write("Please enter a number: ");
    iNumber1 = Convert.ToInt32(Console.ReadLine());
    Console.Write("Please enter a number: ");
    iNumber2 = Convert.ToInt32(Console.ReadLine());

    isValid = areValid(iNumber1, iNumber2);
    if(!isValid)
    {
       Console.WriteLine("Out of range calculation is ignored");
    }
}

现在您知道自己有两个有效数字,可以继续执行程序的其余部分。

答案 1 :(得分:0)

public class Program
{
    public static void Main(string[] args)
    {       
        Console.WriteLine("Please give me 2 number between -10 and +10 and ill add them together\n");
        int iNumber1 = readNumber();
        int iNumber2 = readNumber();

        int iResult = iNumber1 + iNumber2;

        Console.WriteLine("The sum of {0} + {1} is {2}", iNumber1, iNumber2, iResult);
    }

    //you should use functions to avoid code repetition.
    //the function below reads the user input and tries to convert it into a number.
    //the validation rule is applied inside the loop.
    private static int readNumber()
    {
        bool bUserInput;
        int number;

        //below is a loop that runs at least once. the loop continues
        //iterating while the condition evaluates to true, otherwise it ends
        //and control goes to the statement immediately after it. 
        do
        {
            Console.WriteLine("Please enter a number: ");
            //Int32.TryParse receives as arguments a string to parse and
            //an integer as an output argument (you need the out keyword)
            //it returns true if it the string could be successfully converted
            //into an integer and false if it couldn't.
            bUserInput = Int32.TryParse(Console.ReadLine(), out number);
            if (!bUserInput)//this will be true if the user input could not be converted
            {
                Console.WriteLine("Input could not be converted into an integer number");
                //the continue statement is used inside a loop to skip the rest of it
                //and jump directly to the test condition
                continue;
            }
            if (number < -10 || number > 10)//the validation
            {
                //the error message
                Console.WriteLine("Your are out of range please stay between -10 and +10");
                //make sure we set the flag to false because in this case, 
                //Int32.TryParse has set it to true, but input is still invalid
                //by the validation rules.
                bUserInput = false;
            }
            else//the number is in range, set the flag to true
            {
                Console.WriteLine("Good Job!\n");
                //set the flag to true, so the loop can end.
                bUserInput = true;
            }
        }while(!bUserInput);//while this evaluates to true, the loop continues.

        return number;
    }                   
}
相关问题