Mam problem z zapętleniem instrukcji if w moim kodzie. Spojrzałem na inne wątki na stackoverflow, ale nie mogłem go uruchomić wiele razy. Program, który próbuję stworzyć, jest podstawowym konwerterem dla firmy odlewniczej. Próbowałem zrobić to tak, aby użytkownik mógł wprowadzić rodzaj konwersji potrzebnej, a następnie waga wosku. Dałoby to użytkownikowi odpowiednią ilość gramów metalu szlachetnego do użycia. Problem polega na tym, że muszę go uruchamiać od początku, aż użytkownik skończy z niego korzystać. Próbowałem użyć instrukcji while, ale po prostu pętli inną część instrukcji if. Oto mój kod dla odniesienia:Jak zapętlić instrukcję if z wieloma innymi, jeśli warunki
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();
}
}
}
Zdaję sobie sprawę, że dobry programista nie wpisać ten sam kod dwa razy, ale po prostu nie jestem na tym poziomie jeszcze, po prostu chcę kod do pętli. Czy musiałbym zrobić duże zagnieżdżone instrukcje?
Właściwie, jestem trochę zazdrosny, że to pytanie z łatwością może zdobyć trzy punkty. –
@UweKeim Cóż, widząc odpowiedzi ... to niesamowite, jak łatwo jest tak wielu ludziom całkowicie przegapić punkt na tak proste pytanie i taki prosty kawałek kodu. – Luaan