0
Coffee Time exercise from c# dictionary, hint 🤔
Hey i see that this quest have hint "use coffee.Keys.ToArray() inside foreach loop" but i dont understand why. I tried to solve this quest and i pass but i put coffe.Keys.ToArray() outside foreach loop how is possible to use it inside? 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); string[] coffeName = coffee.Keys.ToArray(); foreach(string key in coffeName) { Console.WriteLine("{0} : {1}",key,Math.Round(coffee[key]*(Convert.ToDouble(100-discount)/100),MidpointRounding.AwayFromZero)); } Console.ReadKey();
9 Answers
+ 4
Mr Wolfy, I'm using the following loop => foreach (string c in coffee.Keys.ToArray()){
//performing calculations
}
+ 9
foreach (string s in coffee.Keys.ToArray())
{
coffee[s] -= (coffee[s]*discount)/100;
Console.WriteLine(s+": "+coffee[s]);
}
+ 5
Did the same of Wolfy with this inside
coffee[s] -= (coffee[s]*discount)/100;
+ 1
wow many interesting and complicated calculations but I just want to add mine;
///////////////
foreach (string cf in coffee.Keys.ToArray())
{
a = coffee[cf] * discount;
a = a / 100;
coffee[cf] -= a;
Console.WriteLine("Value; {0}", coffee[cf]);
}
+ 1
its simple and easy
foreach(string s in coffee.Keys){
int a = coffee[s]- coffee[s]*discount/100;
Console.WriteLine(s+":"+" "+a);
0
O thanks i didnt know that i can use ToArray() in this way :o
0
This is a nice challenge. I completed it similar to you guys and it passed example 1 and 2 but failed example 3 which is locked. So I can't see what is what. I pasted in the code at the top for fun. It's showing failing all 3 test cases despite having the right output. I think this one is bugged.
0
foreach(KeyValuePair<string, int> entry in coffee)
{
Console.WriteLine(entry.Key +": "+(entry.Value-entry.Value*discount/100));
}
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)
{
int discount = Convert.ToInt32(Console.ReadLine());
Dictionary<string, double> coffee = new Dictionary<string, double>();
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())
{
coffee[s] -= (coffee[s]*discount)/100;
Console.WriteLine(s+": "+coffee[s]);
}
}
}
}