0

Get that discount - practice c#

I have an issue to solve this practice in c#. Can you check my code and let me know what is wrong. I guess you are already familiar with this task, the discount is given in case that the total amount is 10k or more. using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int totalPrice = Convert.ToInt32(Console.ReadLine()); //call the method Discount; } //complete the method declaration static int Discount() { //complete the method body if (totalPrice >= 10000) { totalPrice = totalPrice -(0.2*totalPrice); Console.WriteLine(totalPrice); }else { Console.WriteLine(totalPrice); } } } }

4th Aug 2021, 11:09 PM
Ivan
Ivan - avatar
7 Respuestas
0
You need to repeat the lesson passed for this task, as you have made too many mistakes.
5th Aug 2021, 12:13 AM
Solo
Solo - avatar
0
Ok, thanks. Can you point at least at biggest one? I'm aware that for most tasks there are a few ways to be solved. Thus I need to know did I completely missed out the point or not. Thanks
5th Aug 2021, 6:52 AM
Ivan
Ivan - avatar
0
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int totalPrice = Convert.ToInt32(Console.ReadLine()); //call the method Discount; //bug 1 } //complete the method declaration static int Discount() //bug 2 { //complete the method body if (totalPrice >= 10000) { totalPrice = totalPrice -(0.2*totalPrice); //bug 3 Console.WriteLine(totalPrice); }else { Console.WriteLine(totalPrice); }
5th Aug 2021, 7:12 AM
Solo
Solo - avatar
0
Вам нужно написать функцию, (здесь почему-то называют её методом), с параметром, (аргументом целочисленного типа) и соответственно вызывать функцию с аргументом, иначе функция не видит переменную totalPrice так как она определена вне функции. В C# нельзя дробные числа вычислять с целыми числами без конвертации, то есть привидению к единому типу чисел. Я не знаю данное задание, так как Про - версия мне не доступна, но если вывод должен быть целым числом, то можно просто переписать вычисление так: 2/10 * totalPrice
5th Aug 2021, 7:48 AM
Solo
Solo - avatar
0
Спасибо!
5th Aug 2021, 8:19 AM
Ivan
Ivan - avatar
0
Hi everyone, as a beginner I had a headache from this. Especially because originally there was an deceiving thing in the original method declaration. See below. using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(String[] args) { int totalPrice = Convert.ToInt32(Console.ReadLine()); //call the method Discount(totalPrice); } //complete the method declaration static void Discount(int totalPrice) // !! here it was originally static int Discount() !! it must be static Void { //complete the method body if (totalPrice >= 10000) { double dis = totalPrice - (0.2 * totalPrice); totalPrice = Convert.ToInt32(dis); Console.WriteLine(totalPrice); } else { Console.WriteLine(totalPrice); } } } }
15th Jan 2022, 8:44 AM
Mgr. art. Peter Varga
0
Thank you for clarification of each part of the code. All makes sense.
17th Jan 2022, 7:48 AM
Ivan
Ivan - avatar