+ 5
Coffee Time - C# The Last Excerise
https://www.sololearn.com/coach/1054?ref=app I have attempted this but I can't figure out how to work out the discount of the <int> value in the dictionary "coffee" and how to output this.
33 Antworten
+ 48
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 (KeyValuePair<string, int> entry in coffee)
{
Console.WriteLine(entry.Key + ": " + (entry.Value - entry.Value * discount / 100));
}
}
}
}
+ 12
Yes, I updated my code and now it passes the unit tests. Thanks XXX MrDevEzeoke for the support. Anyway I think the scenario given in that exercise should have been little bit more clearer.
+ 4
I also have a problem with the exercise. I added the code I wrote for the exercise but unit tests fail. I don't get how the discount works there.
+ 3
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 (var s in coffee.Keys.ToArray())
{
coffee[s] = coffee[s] - coffee[s] * discount/100;
Console.WriteLine(s + ": "+ coffee[s]);
}
}
}
}
That´s a possible way to solve this problem using coffee.Keys.ToArray() in the foreach loop for those who asked:)
+ 3
try this code its work
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(KeyValuePair<string,double>entry in coffee)
{
Console.WriteLine(entry.Key+": " +(entry.Value - entry.Value * discount/100));
}
}
}
}
+ 2
has anyone solved the Drawing Application Challenge in Inheritance and polymorphism
+ 2
Just wondering why nobody has used the coffee.Keys.ToArray() in their solutions?
+ 2
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+": {0}",coffee[s]-coffee[s]*discount/100);
}
}
}
}
+ 1
Oh. Thank you man.
+ 1
Tharinda Nimnajith the input represents discount percentage, not the discount itself.
+ 1
~DevEzeoke16[BTS]~ Thanks a lot for this answer! My answer was correct in 95% of the cases, which was really bugging me ^^
+ 1
Why do we use coffee.Keys.ToArray() instead of coffee.keys
????
0
MrDevEzeoke
Read the question again. It says that a discount value is given as input and you have to output the price of each coffee type AFTER APPLYING THE DISCOUNT.
So for example, if the input is 10, you will apply 10% discount on each coffee type. In the end the cost of
Americano = 50 - 10% of 50 = 45
and so on
0
Tharinda Nimnajith yeah what XXX said I just realised but thank you for the answer as it will help me perform the new value.
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, 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);
foreach(var c in coffee)
Console.WriteLine(quot;{c.Key}: {c.Value-c.Value*discount/100}");
}
}
}
0
Ewout Lagendijk Your welcome mate.
0
@Richie Ro binson
Im wondering aswell.
I didn't use coffee.Keys.ToArray() either.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Coffee_Time
{
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", 30);
coffee.Add("Cappucino", 90);
coffee.Add("Mocha", 100);
foreach (KeyValuePair<string, int>d in coffee)
{
int zahl = d.Value * discount / 100;
Console.WriteLine(quot;{d.Key}: {d.Value - zahl} ");
}
}
}
}
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, 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[] coffeename = coffee.Keys.ToArray();
foreach (string key in coffeename)
{
Console.WriteLine("{0} : {1}", key, Math.Round(coffee[key] * (Convert.ToDouble(100 - discount) / 100), MidpointRounding.AwayFromZero));
}
}
}
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, 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 (KeyValuePair<string, int> entry in coffee)
{
Console.WriteLine(entry.Key + ": " + (entry.Value - entry.Value * discount / 100));
}
}
}
}