How to encode an if statement with many others if conditions

I have a problem with if-statement loop in my code. I looked at other threads in stackoverflow, but I could not get it to work several times. The program that I am trying to create is the basic converter for the foundry company. I tried to make it so that the user can enter the type of transformation needed then the weight of the wax. He will then provide the user with the correct amount of grams of precious metal to use. The problem is that I need it to start from the very beginning, until the user has executed it. I tried using the while statement, but it just iterates over the other part of the if-statement. Here is my code for reference:

static void Main(string[] args) { double waxWeight, bronzeWeight, silverWeight, fourteenkGoldWeight, eighteenkGoldWeight, twentytwokGoldWeight, platinumWeight; string wW; bool doesUserWantToLeave = false; Console.WriteLine("Please specify the type of conversion you would like to accomplish:" + "\n(Bronze, Silver, 14k Gold, 18k Gold, 22k Gold, Platinum, or Exit):"); string conversionType = Console.ReadLine(); //bool B = conversionType == "Bronze"; //bool S = conversionType == "Silver"; //bool ftG = conversionType == "14k Gold"; //bool etG = conversionType == "18k Gold"; //bool ttG = conversionType == "22k Gold"; //bool P = conversionType == "Platinum"; while (!doesUserWantToLeave) { if (conversionType == "Bronze") { Console.WriteLine("What is the weight of the wax model?"); wW = Console.ReadLine(); waxWeight = double.Parse(wW); bronzeWeight = waxWeight * 10; Console.WriteLine("You need " + bronzeWeight + " grams of bronze."); Console.ReadLine(); } else if (conversionType == "Silver") { Console.WriteLine("What is the weight of the wax model?"); wW = Console.ReadLine(); waxWeight = double.Parse(wW); silverWeight = waxWeight * 10.5; Console.WriteLine("You need " + silverWeight + " grams of silver."); Console.ReadLine(); } else if (conversionType == "14k Gold") { Console.WriteLine("What is the weight of the wax model?"); wW = Console.ReadLine(); waxWeight = double.Parse(wW); fourteenkGoldWeight = waxWeight * 13.5; Console.WriteLine("You need " + fourteenkGoldWeight + " grams of 14 Karat gold."); Console.ReadLine(); } else if (conversionType == "18k Gold") { Console.WriteLine("What is the weight of the wax model?"); wW = Console.ReadLine(); waxWeight = double.Parse(wW); eighteenkGoldWeight = waxWeight * 15; Console.WriteLine("You need " + eighteenkGoldWeight + " grams of 18 Karat gold."); Console.ReadLine(); } else if (conversionType == "22k Gold") { Console.WriteLine("What is the weight of the wax model?"); wW = Console.ReadLine(); waxWeight = double.Parse(wW); twentytwokGoldWeight = waxWeight * 17.3; Console.WriteLine("You need " + twentytwokGoldWeight + " grams of 22 Karat gold."); Console.ReadLine(); } else if (conversionType == "Platinum") { Console.WriteLine("What is the weight of the wax model?"); wW = Console.ReadLine(); waxWeight = double.Parse(wW); platinumWeight = waxWeight * 21.5; Console.WriteLine("You need " + platinumWeight + " grams of platinum."); Console.ReadLine(); } else if (conversionType == "Exit") { doesUserWantToLeave = true; } else { Console.WriteLine("Sorry! That was an invalid option!"); Console.ReadLine(); } } } 

I understand that a good programmer does not enter the same code twice, but I'm not at this level yet, I just want the code to loop. Should I make it a big nested if-statement?

+6
source share
3 answers

You only ask for a metal type once. Move the two lines where you request and get the user input inside the while :

 while (!doesUserWantToLeave) { Console.WriteLine("Please specify the type of conversion you would like to accomplish:" + "\n(Bronze, Silver, 14k Gold, 18k Gold, 22k Gold, Platinum, or Exit):"); string conversionType = Console.ReadLine(); if (conversionType == "Bronze") { ... 

You mentioned that you are new to programming and know that you are repeating yourself. You prioritize correctly to get code working first. It's good. Once you get it working, you should look at the code improvement.

Firstly, after each if following three lines are repeated, so they need to be set only once at the top of the loop:

 Console.WriteLine("What is the weight of the wax model?"); wW = Console.ReadLine(); waxWeight = double.Parse(wW); 

Further, the last two lines in each if are largely repeated, but the only part that changes (metal name) is known. Therefore, all of them can be deleted and replaced with only one copy at the end of the cycle:

 Console.WriteLine("You need " + metalWeight + " grams of {0}.", conversionType.ToLower()); Console.ReadLine(); 

Then it just leaves one line for if . It is also repeated, and the necessary values ​​can be stored in the dictionary. Do all this and you might get a solution like:

 static void Main(string[] args) { bool userWantsToStay = true; var conversions = new Dictionary<string, double> { { "Bronze", 10.0 }, { "Silver", 10.5 }, { "14k Gold", 13.5 }, { "18k Gold", 15.0 }, { "22k Gold", 17.3 }, { "Platinum", 21.5 } }; while (userWantsToStay) { Console.WriteLine("Please specify the type of conversion you would like to accomplish:"); Console.WriteLine("(Bronze, Silver, 14k Gold, 18k Gold, 22k Gold, Platinum, or Exit):"); var metalType = Console.ReadLine(); Console.WriteLine("What is the weight of the wax model?"); var wW = Console.ReadLine(); var waxWeight = double.Parse(wW); if (conversions.ContainsKey(metalType)) { var metalWeight = waxWeight * conversions[metalType]; Console.WriteLine("You need {0} grams of {1}.", metalWeight, metalType.ToLower()); Console.ReadLine(); } else if (metalType == "Exit") { userWantsToStay = false; } else { Console.WriteLine("Sorry! That was an invalid option! Try again"); Console.ReadLine(); } } } 

This can be further improved (many ReadLines can be removed, you do not check if the weight entry is a valid double before parsing), but it will set you on the right path.

+5
source

You need to reassign the user option at the end of the loop, otherwise it will never change:

 while (!doesUserWantToLeave) { if (conversionType == "Bronze") { //.... } // ... else if (conversionType == "Exit") { doesUserWantToLeave = true; } else { Console.WriteLine("Sorry! That was an invalid option!"); } conversionType = Console.ReadLine(); } 

Thus, you must also delete all the other Console.ReadLine(); in a loop. You can also use break instead of doesUserWantToLeave = true , which leaves the loop.

+3
source

You can extract this piece of code into a separate method that will be called from the main program loop, see below pseudocode:

 public void Main(string[] args) { while(!doesUserWantToLeave) { Console.WriteLine("Please specify the type of conversion you would like to accomplish:" + "\n(Bronze, Silver, 14k Gold, 18k Gold, 22k Gold, Platinum, or Exit):"); string conversionType = Console.ReadLine(); Console.WriteLine("What is the weight of the wax model?"); double waxWeight = double.Parse(Console.ReadLine()); double weight = ConvertMethod(conversionType, waxWeight); Console.WriteLine(string.Format("You need {0} grams of {1}.", weight, conversionType)); } } 

The method will look as follows. You can either pass strings or use an enumeration to determine conversion types.

 public double ConvertMethod(string type, double weight) { switch(type) { case "Silver": return weight * 10.5; case "Bronze": return weight * 10; // etc... } } 
-1
source

All Articles