Challenge: Safe Parsing for User Input
Tâche
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
priceInputas a decimal. - Attempt to parse
quantityInputas an int. - If both values are invalid, return
"Invalid price and quantity input.". - If only
priceInputis invalid, return"Invalid price input.". - If only
quantityInputis 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.
Solution
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 4
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Génial!
Completion taux amélioré à 4.17
Challenge: Safe Parsing for User Input
Glissez pour afficher le menu
Tâche
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
priceInputas a decimal. - Attempt to parse
quantityInputas an int. - If both values are invalid, return
"Invalid price and quantity input.". - If only
priceInputis invalid, return"Invalid price input.". - If only
quantityInputis 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.
Solution
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 4
single