如何不断索要新 ID 直到它不再被使用
How to keep asking for a new ID until it is no longer already taken
我当前的代码存在问题,不确定如何在 ID 长度 != 8 且 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 号是否尚未被使用,因为当用户查找时,这将是一个错误。
想想你自己说的话
Keep looping through my code while the ID length != 8 and the number
is taken
然后随机播放你手头的东西。
我也冒昧地放弃了 ref 参数,请随意放回去
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.....
}
我实际上没有运行这个所以你可能需要错误检查。
您可以使用此代码。它会一直询问 ID,直到用户输入一个尚未使用的 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
好的,对于那些感兴趣的人,@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;
}
我当前的代码存在问题,不确定如何在 ID 长度 != 8 且 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 号是否尚未被使用,因为当用户查找时,这将是一个错误。
想想你自己说的话
Keep looping through my code while the ID length != 8 and the number is taken
然后随机播放你手头的东西。
我也冒昧地放弃了 ref 参数,请随意放回去
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.....
}
我实际上没有运行这个所以你可能需要错误检查。
您可以使用此代码。它会一直询问 ID,直到用户输入一个尚未使用的 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
好的,对于那些感兴趣的人,@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;
}