+ 3

C# Coffee Time

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) { int discount = Convert.ToInt32(Console.ReadLine()); Dictionary<string, int> coffee = new Dictionary<string, int>(); coffee.Add("Americano", 50); coffee.Add("Latte", 70); coffee.Add("Flat White", 60); coffee.Add("Espresso", 60); coffee.Add("Cappuccino", 80); coffee.Add("Mocha", 90); //your code goes here foreach(string s in coffee.Keys.ToArray()){ Console.WriteLine(s+": "+Convert.ToInt32(coffee[s]-coffee[s]*Convert.ToDouble(discount)/100+0.01)); } } } } Although this passes the unit test but is there a lazier way to get around this long and messy code?

14th Aug 2021, 5:59 AM
Moses Loh
Moses Loh - avatar
3 Respuestas
+ 4
I did it this way, but I wouldn't say it was a lazier method, just slightly different. //your code goes here foreach (string item in coffee.Keys.ToArray()) { double rebate = (coffee[item]*discount)/100; Console.WriteLine(item +": "+ (coffee[item] - rebate)); }
14th Aug 2021, 8:18 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Change foreach to foreach(KeyValuePair<string, int> entry in coffee) { Console.WriteLine(entry.Key +": "+(entry.Value-entry.Value*discount/100)); }
6th Mar 2022, 9:16 PM
Jackie
Jackie - avatar
+ 1
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) { double discount = Convert.ToDouble(Console.ReadLine()); Dictionary<string, double> coffee = new Dictionary<string, double> { { "Americano", 50}, { "Latte", 70 }, { "Flat White", 60 }, { "Espresso", 60 }, { "Cappuccino", 80 }, { "Mocha", 90 } }; foreach (var drink in coffee.Keys.ToArray()) { double rebate = (coffee[drink] * discount) / 100.0; double newPrice = (double)(coffee[drink] - rebate); Console.WriteLine(drink + ": " + newPrice); } } //This code its better, and you should try somthing different } }
14th Oct 2023, 11:27 PM
Claudeen Jimenez
Claudeen Jimenez - avatar