Challenge: Safe Parsing for User Input
Taak
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.
Oplossing
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 3. Hoofdstuk 4
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 4.17
Challenge: Safe Parsing for User Input
Veeg om het menu te tonen
Taak
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.
Oplossing
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 3. Hoofdstuk 4
single