2013-09-25 88 views
0

我的問題是我如何驗證SIN用戶輸入到我的程序中我似乎無法得到IF來打印它是否有效,我也不確定我如何能夠這樣當輸入的SIN無效時,程序再次要求犯罪,但如果第二次無效,程序應該關閉。驗證C#中的用戶輸入

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(); 
     } 
    } 
} 
+0

邊注:您的SAM ple非常長 - 請查看http://www.sscce.org以瞭解將來的問題。 –

回答

0

爲什麼你需要所有那些Console.ReadLine()?如果你調試你的代碼,你會看到它在打印結果之前停下來從控制檯讀取一些東西(兩次!)。

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

您應該閱讀SIN,驗證它,打印結果。

如果要驗證第二SIN如果第一個失敗了,你可以做這樣的事情:

int numberOfValidations = 0; 
bool validSIN = false; 

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

numberOfValidations++; 

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

編輯

這裏有Ideone一個工作演示與您的代碼:

DEMO

+0

好吧我刪除了多餘的readlines,但我仍然有問題,它不會驗證罪 – Shahze123

+0

@ Shahze123我已經測試了您的代碼,沒有不必要的讀取線,它驗證SIN打印輸出。嘗試做一些調試來了解你的條件如何。 –

+0

@ Shahze123我已經編輯了我的回覆,在代碼中加入了一個Demo(如果第一次失敗,請檢查第二個SIN)。它驗證兩個SIN並輸出驗證結果。 –