Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Safe Parsing for User Input | Validating Input and Safe Parsing
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Exceptions and Error Handling Practice

bookChallenge: Safe Parsing for User Input

Aufgabe

Swipe to start coding

Implement a method that safely parses string input for a product price and quantity. Your method should use TryParse to convert the price to a decimal and the quantity to an int, and return a message indicating the result.

  • Attempt to parse priceInput as a decimal.
  • Attempt to parse quantityInput as an int.
  • If both values are invalid, return "Invalid price and quantity input.".
  • If only priceInput is invalid, return "Invalid price input.".
  • If only quantityInput is invalid, return "Invalid quantity input.".
  • If both values are valid, return a message in the format: "Total cost for {quantity} items at {price:C} each is {total:C}.", where {price} is the parsed decimal, {quantity} is the parsed int, and {total} is the product of price and quantity.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 4
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain this in simpler terms?

What are the main benefits or drawbacks?

Can you give me a real-world example?

close

bookChallenge: Safe Parsing for User Input

Swipe um das Menü anzuzeigen

Aufgabe

Swipe to start coding

Implement a method that safely parses string input for a product price and quantity. Your method should use TryParse to convert the price to a decimal and the quantity to an int, and return a message indicating the result.

  • Attempt to parse priceInput as a decimal.
  • Attempt to parse quantityInput as an int.
  • If both values are invalid, return "Invalid price and quantity input.".
  • If only priceInput is invalid, return "Invalid price input.".
  • If only quantityInput is invalid, return "Invalid quantity input.".
  • If both values are valid, return a message in the format: "Total cost for {quantity} items at {price:C} each is {total:C}.", where {price} is the parsed decimal, {quantity} is the parsed int, and {total} is the product of price and quantity.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 4
single

single

some-alt