Sajo
Member
- Joined
- Jul 22, 2020
- Messages
- 17
- Programming Experience
- Beginner
Hello everyone. I solved the task correctly. What worries me is whether my solution takes up too much memory. In case anyone knows a better solution please write. I am currently studying complex conditions and conditional statements.
Here is the text of task.
Here is my code
My code is working but if anyone knows better way, please can you write me?
Here is the text of task.
Here is example (Sorry for writing in Excel but it is easiest way for me to show you)The store sells three types of chocolate. For each type of chocolate we know how many grams there are and how much it costs. Write a C # program that for each chocolate loads from the user the name of the chocolate mass and price and determines which is the most cost-effective. The most cost-effective chocolate is the one that gets the most chocolate for the least money.
Here is my code
C#:
namespace aplikacija
{
class Program
{
static void Main(string[] args)
{
string chocolate1, chocolate2, chocolate3;
int weight1, weight2, weight3, price1, price2, price3;
Console.WriteLine(" \a Hello everybody, today we will choose which chocolate you will buy :D \n Enter information about chocolates\n Enter the names of the chocolates ");
chocolate1 = Console.ReadLine();
chocolate2 = Console.ReadLine();
chocolate3 = Console.ReadLine();
Console.WriteLine(" Enter the weight of the chocolates ");
weight1 = int.Parse(Console.ReadLine());
weight2 = int.Parse(Console.ReadLine());
weight3 = int.Parse(Console.ReadLine());
Console.WriteLine(" Enter the prices of the chocolates");
price1 = int.Parse(Console.ReadLine());
price2 = int.Parse(Console.ReadLine());
price3 = int.Parse(Console.ReadLine());
Console.WriteLine(String.Format("Here is the entered data\n Name of chocolate:\t{0}\t{1}\t{2} \n Weight of chocolate:\t{3} g\t{4} g\t{5} g\n Price of chocolate:{6} $\t{7} $\t{8} $", chocolate1, chocolate2, chocolate3, weight1, weight2, weight3, price1, price2, price3));
if (((weight1 > weight2) && (weight1 > weight3)))
{
Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate1,weight1,price1));
}
else if (((weight2 > weight1) && (weight2 > weight3)))
{
Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate2, weight2, price2));
}
else if (((weight3 > weight1) && (weight3 > weight2)))
{
Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate3, weight3, price3));
}
else if ((price1<price2) && (price1<price3))
{
Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate1, weight1, price1));
}
else if ((price2 < price1) && (price2 < price3))
{
Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate2, weight2, price2));
}
else if ((price3 < price1) && (price3 < price2))
{
Console.WriteLine(String.Format("The most cost-effective chocolate is {0} the weight is {1} and price is {2} ", chocolate3, weight3, price3));
}
}
}
}