2016-06-09 81 views
-1
int anotherShowing = 1; // Variable for the user to select another showing. 

     while (anotherShowing == 1) // While loop so the program keeps going until the user has selected a film. 
     { 
      Console.Write("Select A Showing: "); // Outputs "Select A showing" to the console window. 
      int showingSelect = Console.ReadLine(); // Variable for the user to input a showing number and converts it to a int. 
      int.TryParse(anotherShowing, out showingSelect); 
      if (showingSelect == 0) 
      { 
       Console.Write("please input valid number"); 
      } 
      Showing Selection; // Variable so Selection can be linked to the Showing class to be used for the ShowingSelect. 
      Selection = list[showingSelect - 1]; // Recongnizes the showing number the user inputs. 
      Console.Write("Number of Tickets: "); // Oupts "Number of Tickets" to the console window. 
      int numberOfTickets = Convert.ToInt32(Console.ReadLine()); // Variable for the user to input how many tickets they want and converts it to a int. 
      Booking Booking1; // Variable so Booking1 can be linked to the Booking class to be used for the NumberOfTickets. 
      if (Selection.getSeatsAvailable() > numberOfTickets) // If statement so the program knows if there are enough tickets left. 
      { 
       Booking1 = new Booking(Selection, numberOfTickets); // If enough tickets program will make a new booking (Booking1) which includes the Selection and the NumberOfTickets. 
       Selection.updateSeatsAvailable(numberOfTickets); // Updates how many tickets are available for that showing. 
       Console.Clear(); // Clears the console window. 
       Console.Write(Booking1.toString()); // Outputs the Booking1 variable, including the Film selected and the number of tickets. 
       break; // Stops the program. 
      } 
      else // If there aren't enough tickets. 
      { 
       Console.WriteLine("Showing Full"); // Outputs "Showing Full" to the console window. 
       Console.WriteLine("Select another showing"); // Outputs "Select another showing" to the console window. 
      } 

      Console.Write("Press 1 Then Return To Select Another Showing\n"); // Outputs to the console window. 
      anotherShowing = Convert.ToInt32(Console.ReadLine()); // Allows the user to input 1 to select another showing and converts it to a int. 
     } 

     Console.Read(); 
    } 

我想写一个简单的电影程序作业。 我看了一些关于如何告诉用户输入一个字母而不是数字的例子,如果他们输入一个数字。 我已经加入的TryParse但也存在一些误区:当用户输入一个字母而不是数字

errors

请码帮忙, 感谢

+1

的[我怎么只允许数字输入到我的C#控制台应用程序?(可能的复制http://stackoverflow.com/questions/13106493/how-do-i-only-allow-number-input -int-my-c-sharp-console-application) – BunkerMentality

回答

2

Console.ReadLine()返回一个stringint值。

所以我们应该这样做。

int showingSelect; 

if(int.TryParse(Console.ReadLine(), out showingSelect)) 
{ 
    // valid showingSelect 
} 
else 
{ 
    // invalid input 
    Console.Write("please input valid number"); 
    continue; // continue loop. 
} 
+0

你的意思是// valid showingSelect是什么意思? – Tim

+0

表示您输入了有效的“int”值。 –

+0

将您的逻辑放在该块中。 –

相关问题