在C#中验证用户输入

时间:2013-09-25 02:31:31

标签: c# validation console

我的问题是我如何验证用户输入到我的程序中的SIN我似乎无法获得IF打印它是否有效,我也不确定我是如何做到这样的,当输入的SIN无效,程序再次询问罪,但如果第二次程序关闭则无效。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sinValidator
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variables


            Int32 numbers1, numbers2, numbers3, numbers4, numbers5, numbers6, numbers7, numbers8, numbers9;
            Int32 sin;
            bool valid;
            Int32 sum;
            Int32 product;
            double test;

            {

                // Prompt for user input
                Console.WriteLine("\n Enter SIN: ");
                sin = Int32.Parse(Console.ReadLine());

                // spliting the SIN into 9 seperate digits in different variables, and then printing them.

                Console.WriteLine("The first digit is {0}", numbers1 = (sin / 100000000));

                Console.WriteLine("The second digit is {0}", numbers2 = (sin % 100000000 / 10000000));

                Console.WriteLine("The third digit is {0}", numbers3 = (sin % 10000000 / 1000000));

                Console.WriteLine("The fifth digit is {0}", numbers4 = (sin % 1000000 / 100000));

                Console.WriteLine("The sixth digit is {0}", numbers5 = (sin % 100000 / 10000));

                Console.WriteLine("The seventh digit is {0}", numbers6 =(sin % 10000 / 1000));

                Console.WriteLine("The eighth digit is {0}", numbers7 = (sin % 1000 / 100));

                Console.WriteLine("The ninth digit is {0}", numbers8 =(sin % 100 / 10));

                Console.WriteLine("The tenth digit is {0}", numbers9 = (sin % 10));

                // Beginning of validation 
                //obtaining product of the sin using provided multipliers

                product = ((numbers1 * 1) + (numbers2 * 2) + (numbers3 * 1) + (numbers4 * 2) + (numbers5 * 1) + (numbers6 * 2) + (numbers7 * 1) + (numbers8 * 2));
                Console.WriteLine("The product {0}", product);

                // using div and mod to get numbers for the sum of digits

                sum = (((numbers1 * 1)+ (numbers2 * 2) % 10) +((numbers2 * 2) / 10) + (numbers3 * 1) + ((numbers4 * 2)%10) + ((numbers4 * 2)/10) + (numbers5 * 1) + ((numbers6 * 2)%10)+((numbers6 * 2)/10) + (numbers7 * 1) + ((numbers8 * 2)%10) + ((numbers8 * 2)/10));


                //Console.WriteLine("sum = {0}", sum);
                //Test to see if sum % 10 subtracted by 10 will give final digit

                test = (10 -(sum % 10));
               // Console.WriteLine("{0}", test);
                test = double.Parse(Console.ReadLine());



                Console.ReadLine();
                //Display the results
                //Console.WriteLine("first = {0}, second = {1}, third = {2}, fourth = {3}, fifth = {4}, sixth = {5}, seventh = {6}, eighth = {7}, ninth = {8}",
                             //numbers1, numbers2, numbers3, numbers4, numbers5, numbers6, numbers7, numbers8, numbers9);

                valid = false;
                if (test > numbers9)
                {
                    if (test == numbers9)
                        valid = true;
                }

                Console.WriteLine("{0}", valid);

                valid = false;
                if (numbers9 > test && test < numbers9)
                {
                    valid = true;
                }



                Console.WriteLine("{0}", valid);

                Console.ReadLine(); 
        }



            Console.ReadLine();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

为什么你需要所有那些Console.ReadLine()?如果您调试代码,在打印结果之前,您将看到它停在那里从控制台读取内容(两次!)。

...
test = (10 -(sum % 10));
// Console.WriteLine("{0}", test);
test = double.Parse(Console.ReadLine());
Console.ReadLine();
...

您应该阅读SIN,验证它,打印结果。

如果要在第一次SIN失败时验证第二个SIN,您可以执行以下操作:

int numberOfValidations = 0;
bool validSIN = false;

do{
// read SIN
// validate
// print result

numberOfValidations++;

}while( !validSIN || numberOfValidations <= 2);

修改

以下是使用您的代码在Ideone上进行的演示:

DEMO