Robot Barman in C# Intermediate | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Robot Barman in C# Intermediate

Problem You have a robot-barman and his goal is to neatly arrange drinks on the shelves of the bar. He is very smart and takes as many drinks as necessary to evenly distribute them on the shelves, but still has problems with division. The program installed in the robot takes the number of drinks: by dividing the number of drinks by the number of shelves and outputting the result. The program must, also, handle those two, possible problems: 1. The divider (the number of shelves) should never be zero 2. Both inputs should be integers. For the first exception, the program should output "At least 1 shelf", and, for the second, "Please insert an integer". Sample Input 6 two Sample Output Please input an integer For Your Information Use DivideByZeroException for the first exception and FormatException for the second exception. Note: What's not added in my code is the test for the number of shelves should never be zero hoping that DivideByZeroException will catch it but doesn't, possibly.

19th Mar 2024, 5:19 PM
Michael
5 Answers
+ 1
Your code is perfect but it contains one too many closing brace } at the end, which causes a compilation error.
19th Mar 2024, 6:38 PM
Tibor Santa
Tibor Santa - avatar
+ 1
I had a funny feeling that this recipe code had too many braces. But, I, also felt like the recipe code was asking for the finally statement due to the extra braces or the extra braces were to fool me. Thank you for your suggestion and advice.
20th Mar 2024, 6:05 PM
Michael
+ 1
Thank you, Mr. Tibor Santa, the your advised correction made the recipe code work. Thank you, once again, and please have a nice day.
20th Mar 2024, 6:10 PM
Michael
0
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { try { int drinks = Convert.ToInt32(Console.ReadLine()); int shelves = Convert.ToInt32(Console.ReadLine()); //your code goes here int placement = drinks/shelves; Console.WriteLine(placement); } /* * 1. DivideByZeroException => "At least 1 shelf" * 2. FormatException => "Please insert an integer" */ catch (DivideByZeroException) { Console.WriteLine("At least 1 shelf"); } catch (FormatException) { Console.WriteLine("Please insert an integer"); } } } } }
19th Mar 2024, 5:22 PM
Michael
0
That's the code.
19th Mar 2024, 5:22 PM
Michael