2016-06-09 75 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

將您的邏輯放在該塊中。 –

相關問題