在数组中应用验证
本文关键字:验证 应用 数组 | 更新日期: 2023-09-27 18:04:29
我只是想知道如何将只接受int的验证应用到这段代码中,我是新来的,所以不熟悉c#。
Console.Write("Please enter the number of tickets sold for Adult:(100 for exit)");
int adultTickets = Convert.ToInt32(Console.ReadLine());
while (adultTickets == 100)
{
Console.WriteLine("Good Bye");
Environment.Exit(0);
}
Console.Write("Please enter the number of tickets sold for child:(100 for exit)");
int childTickets = Convert.ToInt32(Console.ReadLine());
while (childTickets == 100)
{
Console.WriteLine("Good Bye");
Environment.Exit(0);
}
int[] arrAmt = new int[] { 30, 20 };
int[] tickets = new int[] { adultTickets, childTickets };
int totalCost = 0;
if (tickets[0] >= 5 && tickets[0] <= 30)
{
totalCost += tickets[0] * arrAmt[0];
Console.Write("The sale amount of tickets for Adult: ${0} 'n", (arrAmt[0] * tickets[0]));
}
else
{
Console.Write("Not Valid 'n");
}
if (tickets[1] >= 5 && tickets[1] <= 30)
{
totalCost += tickets[1] * arrAmt[1];
Console.Write("The sale amount of tickets for child: ${0} 'n", (arrAmt[1] * tickets[1]));
}
Console.WriteLine("The total sale amount: {0}", totalCost);
private static bool GetNumberFromConsole()
{
bool isValid = false;
int input = -1;
while (!isValid)
if (int.TryParse(Console.ReadLine(), out input))
isValid = true;
else
Console.WriteLine("not a number");
Console.WriteLine("number entered:" + input);
return isValid;
}
请注意,这段代码可能会使用大量的压缩和泛化,但这是一个想法,您可以使用int.tryparse():
public void calculate_Tickets()
{
int adult = adult_Tickets();
int child = child_Tickets();
ticket_Prices(adult, child);
}
private int adult_Tickets()
{
Console.Write("Please enter the number of tickets sold for Adult:(100 for exit)");
int adultTickets = 0;
if (int.TryParse(Console.ReadLine(),out adultTickets)){
if (adultTickets == 100)
{
Console.WriteLine("Good Bye");
Environment.Exit(0);
}
else
{
Console.WriteLine("you may only inter numbers");
adult_Tickets();
}
}
return adultTickets;
}
private int child_Tickets() {
Console.Write("Please enter the number of tickets sold for child:(100 for exit)");
int childTickets = 0;
if (int.TryParse((Console.ReadLine(), out childTickets)){
if (childTickets == 100)
{
Console.WriteLine("Good Bye");
Environment.Exit(0);
}
} else
{
Console.WriteLine("you may only inter numbers");
child_Tickets();
}
return childTickets;
}
private void ticket_Prices(int adultTickets, int childTickets) {
int[] arrAmt = new int[] { 30, 20 };
int[] tickets = new int[] { adultTickets, childTickets };
int totalCost = 0;
if (tickets[0] >= 5 && tickets[0] <= 30)
{
totalCost += tickets[0] * arrAmt[0];
Console.Write("The sale amount of tickets for Adult: ${0} 'n", (arrAmt[0] * tickets[0]));
}
else
{
Console.Write("Not Valid 'n");
}
if (tickets[1] >= 5 && tickets[1] <= 30)
{
totalCost += tickets[1] * arrAmt[1];
Console.Write("The sale amount of tickets for child: ${0} 'n", (arrAmt[1] * tickets[1]));
}
Console.WriteLine("The total sale amount: {0}", totalCost);
}
在一个方法中(同样需要大量优化-这是试图尽可能接近原始代码):
private void doTickets()
{
AdultTix:
Console.Write("Please enter the number of tickets sold for Adult:(100 for exit)");
int adultTickets = 0;
while (!int.TryParse(Console.ReadLine(), out adultTickets))
{
Console.WriteLine("You may only enter numbers");
Console.ReadLine();
if (int.TryParse(Console.ReadLine(), out adultTickets))
{
break;
}
}
if (adultTickets == 100)
{
Console.WriteLine("Good Bye");
Environment.Exit(0);
}
if (adultTickets > 0 && (adultTickets < 5 || adultTickets > 30))
{
Console.WriteLine("You may only purchase between 5 and 30 adult tickets. You tried to purchase " + adultTickets.ToString() + ". Please try again.");
goto AdultTix;
}
ChildTix:
Console.Write("Please enter the number of tickets sold for Child:(100 for exit)");
int childTickets = 0;
while (!int.TryParse(Console.ReadLine(), out childTickets))
{
Console.WriteLine("You may only enter numbers");
Console.ReadLine();
if (int.TryParse(Console.ReadLine(), out childTickets))
{
break;
}
}
if (childTickets == 100)
{
Console.WriteLine("Good Bye");
Environment.Exit(0);
}
if (childTickets > 0 && (childTickets < 5 || childTickets > 30))
{
Console.WriteLine("You may only purchase between 5 and 30 child tickets. You tried to purchase " + childTickets.ToString() + ". Please try again.");
goto ChildTix;
}
int[] arrAmt = new int[] { 30, 20 };
int[] tickets = new int[] { adultTickets, childTickets };
int totalCost = 0;
totalCost += tickets[0] * arrAmt[0];
Console.Write("The sale amount of tickets for Adult: ${0} 'n", (arrAmt[0] * tickets[0]));
totalCost += tickets[1] * arrAmt[1];
Console.Write("The sale amount of tickets for child: ${0} 'n", (arrAmt[1] * tickets[1]));
Console.WriteLine("The total sale amount: {0}", totalCost);
}