I have an issue looping the if-statement in my code. I looked at other threads on stackoverflow but I couldn't get it to work multiple times. The program I'm trying to create is a basic converter for a casting company. What I tried to do is make it so that the user can input the type of conversion needed then the weight of the wax. It would then give the user the correct amount of grams of precious metal to use. The issue is that I need it to run from the beginning until the user is done using it. I tried using a while statement but it just loops the else part of the if-statement. Here's 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 realize that a good programmer doesn't type the same code twice but I'm just not at that level yet, I just want the code to loop. Would I have to make it a big nested if-statement?
Aucun commentaire:
Enregistrer un commentaire