【发布时间】:2015-09-28 00:43:54
【问题描述】:
在我的代码中循环 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 语句?
【问题讨论】:
-
其实我有点嫉妒这个问题似乎很容易获得三票。
-
@UweKeim 好吧,看到答案了……令人难以置信的是,这么多人完全错过了这么简单的问题和这么简单的代码段的重点。
标签: c# loops if-statement