2016-12-06 23 views
-1

我的当前代码存在一个问题,我不确定如何在ID长度为8的情况下继续循环我的代码,并且ID号已经存在拍摄。这可能与添加新程序一样简单,但我不确定。如何继续询问一个新的ID直到它不再被采用

static void GetIDInput(ref int ID) 
{ 

    int tempID = 0; 

    while (tempID.ToString().Length != 8) 
    { 
     Console.WriteLine("Please enter your desired ID number"); 
     tempID = Convert.ToInt32(Console.ReadLine()); 
    } 

    ID = tempID; 
} 

static void AddStock() 
{ 

    int stockQuantity = 0; 
    double stockPrice = 0.00; 
    string stockName = ""; 
    string s = ""; // Being Lazy here, to convert to when needed. 
    int tempID = 0; 
    int IDNumber = 0; 
    string lineValues; 
    bool taken = false; 

    GetIDInput(ref tempID); 

    using (StreamReader sr = new StreamReader("Stockfile.txt")) 
    { 
     while (sr.EndOfStream == false && taken != true) 
     { 
      lineValues = sr.ReadLine(); 

      if (lineValues.Contains(tempID.ToString())) 
      { 
       taken = true; 
      } 
      else 
      { 
       taken = false; 
      } 
     } 

     if (taken == false) 
     { 
      IDNumber = tempID; 
     } 
     else if (taken == true) 
     { 
      Console.WriteLine("Sorry this ID is already taken, try again"); 
      // Want to re-loop here but not sure how... 
     } 
    } 


    using (StreamWriter sw = new StreamWriter("Stockfile.txt", true)) 
    { 
     s = IDNumber.ToString(); 
     sw.Write(s + "\t"); // Will only accept an 8 figure digit so is safe to have a single value here. 

     using (StreamWriter sw = new StreamWriter("Stockfile.txt", true)) 
     { 
      s = IDNumber.ToString(); 
      sw.Write(s + "\t"); // Will only accept an 8 figure digit so is safe to have a single value here. 

      while (stockName.Length <= 2) // No fancy brands here...... 
      { 
       Console.Write("Please enter the name of the stock: "); 
       stockName = Console.ReadLine(); 
      } 

      s = stockName; 
      sw.Write(s + "\t"); 

      while (stockQuantity < 1) // Running a small shop here... 
      { 
       Console.Write("Please enter the quanity of stock: "); 
       stockQuantity = Convert.ToInt32(Console.ReadLine()); 
      } 

      s = stockQuantity.ToString(); 
      sw.Write(s + "\t"); 

      while (stockPrice < 0.01) // Running a very small shop.... 
      { 
       Console.Write("Please enter the price of the stock: "); 
       stockPrice = Convert.ToDouble(Console.ReadLine()); 
      } 

      s = stockPrice.ToString(); 
      sw.Write(s + "\t"); 

      sw.WriteLine(); // TO create the new line..... 

     } 

的目的,这是检查该文件中的ID号还没有被作为这将是当用户查找错误。

回答

1

想想的话,你自己说的

请通过我的代码,而ID长度循环!= 8和数量 取

和随机播放,你有什么身边去做。

我冒昧沟裁判PARAM为好,随时把它放回去

static int GetIDInput() 
{ 

    int tempID = 0; 
    bool taken = true; 
    bool isInputValid = false; 

    while (taken == true && isInputValid == false) 
    { 
     Console.WriteLine("Please enter your desired ID number"); 
     tempID = Convert.ToInt32(Console.ReadLine()); 

     if (tempID.ToString().Length != 8) 
     { 
      isInputValid = false; 
      Console.WriteLine("ID number must be 8 digits long.") 
     } 
     else 
     { 
      isInputValid = true; 
     } 

     if (isInputValid) // this wont run if the input wasnt 8 characters, so the loop will restart 
     { 
      using (StreamReader sr = new StreamReader("Stockfile.txt")) 
      { 
       while (sr.EndOfStream == false && taken != true) 
       { 
        lineValues = sr.ReadLine(); 

        if (lineValues.Contains(tempID.ToString())) 
        { 
         taken = true; 
        } 
        else 
        { 
         taken = false; 
        } 
       } 

       if (taken == false) 
       { 
        ID = tempID; 
       } 
       else if (taken == true) 
       { 
        Console.WriteLine("Sorry this ID is already taken, try again"); 
        // statements will lead us back to the while loop and taken == true so it will run again 
       } 
      } 
     } 
    } 

    return tempID; 
} 



static void AddStock() 
{ 
    int stockQuantity = 0; 
    double stockPrice = 0.00; 
    string stockName = ""; 
    string s = ""; // Being Lazy here, to convert to when needed. 
    int IDNumber = 0; 
    string lineValues; 

    IDNumber = GetIDInput(); 

    using (StreamWriter sw = new StreamWriter("Stockfile.txt", true)) 
    { 
     s = IDNumber.ToString(); 
     sw.Write(s + "\t"); // Will only accept an 8 figure digit so is safe to have a single value here. 

     using (StreamWriter sw = new StreamWriter("Stockfile.txt", true)) 
     { 
      s = IDNumber.ToString(); 
      sw.Write(s + "\t"); // Will only accept an 8 figure digit so is safe to have a single value here. 

      while (stockName.Length <= 2) // No fancy brands here...... 
      { 
       Console.Write("Please enter the name of the stock: "); 
       stockName = Console.ReadLine(); 
      } 

      s = stockName; 
      sw.Write(s + "\t"); 

      while (stockQuantity < 1) // Running a small shop here... 
      { 
       Console.Write("Please enter the quanity of stock: "); 
       stockQuantity = Convert.ToInt32(Console.ReadLine()); 
      } 

      s = stockQuantity.ToString(); 
      sw.Write(s + "\t"); 

      while (stockPrice < 0.01) // Running a very small shop.... 
      { 
       Console.Write("Please enter the price of the stock: "); 
       stockPrice = Convert.ToDouble(Console.ReadLine()); 
      } 

      s = stockPrice.ToString(); 
      sw.Write(s + "\t"); 

      sw.WriteLine(); // TO create the new line..... 

     } 

我其实没有运行这个,所以你可能需要错误检查。

+0

Lookss不错,但即使已经有它似乎跳过并直接进入股票名称中的条目,这可能只是我的错误虽然没有起诉 – Robertgold

1

您可以使用此代码。它会不断地问一个ID,直到用户输入一个尚未使用的:

//Get the input 
static int GetIDInput() 
{ 
    int id; 
    do 
    { 
     Console.WriteLine("Please enter your desired ID number"); 
     id = Convert.ToInt32(Console.ReadLine()); 
    } 
    while (isIDAlreadyUsed(id)); 
    return id; 
} 

// Check if ID is already used 
public static bool isIDAlreadyUsed(int IDToCheck) 
{ 
    using (StreamReader sr = new StreamReader("Stockfile.txt")) 
    { 
     while (!sr.EndOfStream) 
     { 
      string lineValues = sr.ReadLine(); 
      if(lineValues.Contains(IDToCheck.ToString()) 
       return true; 
     } 
    } 
    return false; 
} 

static void AddStock() 
{ 
    // Your init 
    // ... 
    int id = GetIDInput(); // Get the ID 

    //... Your logic to apply 
1

奥凯对于那些有兴趣,@ plast1k有原始的代码,只需编辑我的需要。

static int GetIDInput() 
    { 

     int tempID = 0; 
     bool taken = false; 
     bool isInputValid = false; 
     string lineValues; 

     while (isInputValid == false) 
     { 
      Console.WriteLine("Please enter your desired ID number"); 
      tempID = Convert.ToInt32(Console.ReadLine()); 

      if (tempID.ToString().Length != 8) 
      { 
       isInputValid = false; 
       Console.WriteLine("ID number must be 8 digits long."); 
      } 
      else if (tempID.ToString().Length == 8) 
      { 
       isInputValid = true; 
      } 

      if (isInputValid) // this wont run if the input wasnt 8 characters, so the loop will restart 
      { 
       using (StreamReader sr = new StreamReader("Stockfile.txt")) 
       { 
        while (sr.EndOfStream == false && taken != true) 
        { 
         lineValues = sr.ReadLine(); 

         if (lineValues.Contains(tempID.ToString())) 
         { 
          taken = true; 
         } 
         else 
         { 
          taken = false; 
         } 


         if (taken == false) 
         { 

         } 
         else if (taken == true) 
         { 
          Console.WriteLine("Sorry this ID is already taken, try again"); 

          isInputValid = false; 
          // statements will lead us back to the while loop and taken == true so it will run again 
         } 
         } 
       } 
      } 
     } 

     return tempID; 
    } 
相关问题