我有一个问题,就是我的代码中的if语句无法循环。我查看了stackoverflow上的其他帖子,但我无法使它多次运行。我想要创建的程序是一个基本的铸造公司转换器。我试图让用户输入所需的转换类型,然后输入蜡的重量。然后,它会给用户正确的贵金属克数。问题在于,我需要从开始到用户使用完毕都可以运行。我尝试使用while语句,但它只循环if语句的else部分。以下是我的参考代码:
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();
}
}
}
我知道优秀的程序员不会重复输入相同的代码,但我还没有达到那个水平,我只想让代码循环。我需要将它变成一个大的嵌套 if 语句吗?