2016-08-27 4 views
0

이 코드에만 int를 받아들이는 유효성 검사를 어떻게 적용 할 수 있을지 궁금해했습니다. 저는 C#에 익숙하지 않으므로 새로운 것입니다. 하나의 방법에서배열에 유효성 검사 적용

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); 

    } 
+0

코드 벨을 적용하는 방법을 모릅니다. 내 자신으로, 나는 이것에 진짜로 새롭다, 직접적인지도에서 누군가. 감사. –

답변

0
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; 
} 
0

주 (다시 많은 최적화가 필요합니다. 원래 코드에 최대한 가깝게하려고 노력하고 있습니다.)

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

메인 내 모든 작업을 수행 할 수 있습니까? –

+0

사용자가 잘못된 입력을 두 번 이상 입력 할 수 있다는 점을 감안하면 그렇게하는 것이 합리적입니다. 가능한 한 원본 코드에 가깝게 유지하십시오 (다시 말하면 많은 최적화가 발생할 수 있습니다). 한 가지 방법으로 수행하면 다음과 같이 보일 수 있습니다. –

0

:이 코드는 압축 및 일반화를 많이 사용할 수 있지만 이것은 당신이 int.tryparse()를 사용하여 원하는 것을 할 생각이다

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); 
관련 문제