2016-06-09 27 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(); 
    } 

Ich versuche, ein einfaches Kinoprogramm für eine Aufgabe zu schreiben. Ich schaute mir ein Beispiel an, wie man den Benutzer anweist, einen Buchstaben anstelle einer Zahl einzugeben, wenn sie eine Zahl eingeben. Ich habe TryParse jedoch hinzugefügt gibt es einige Fehler:Wenn Benutzer einen Buchstaben anstelle der Nummer eingibt

errors

in Code Bitte helfen Sie, Dank

+1

Mögliches Duplikat [Wie ich nur Zahleneingabe in meine C# Konsole-Anwendung erlauben?] (Http://stackoverflow.com/questions/13106493/how-do-i-only-allow-number-input -in-meine-c-sharp-console-application) – BunkerMentality

Antwort

2

Console.ReadLine() eine stringnicht einen int Wert zurückgibt.

also sollten wir dies tun.

int showingSelect; 

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

was meinst du mit // valide showingSelect? – Tim

+0

bedeutet, dass Sie einen gültigen Int-Wert eingegeben haben. –

+0

Platzieren Sie Ihre Logik in diesem Block. –

Verwandte Themen