c# - 如何使用许多其他 if 条件循环 if 语句

标签 c# loops if-statement

我在代码中循环 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 语句吗?

最佳答案

您只需要一次金属活字。在 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")
    {
...

您提到您是编程新手,并且知道您在重复自己。不过,您正确地优先考虑让代码先工作。这很好。让它工作后,您应该着眼于改进代码。

首先,在每个 if 之后重复以下三行,因此只需要在循环顶部询问一次:

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

接下来,每个 if 中的最后两行大部分重复,但唯一改变的部分(金属名称)是已知的。所以它们都可以被删除并在循环结束时只用一个副本替换:

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

然后每个 if 只留下一行。它也会 self 重复,所需的值可以存储在字典中。完成所有这些,您最终可能会得到如下解决方案:

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();
        }
    }
}

这可以进一步改进(许多 ReadLines 可能会被删除;您没有在解析之前测试权重输入是否是有效的 double 值),但它会让您处于正确的位置路径。

关于c# - 如何使用许多其他 if 条件循环 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31320907/

相关文章:

c# - 自动生成的 Gridview - 更改列宽

c# - Powershell 如何知道在哪里可以找到要导入的模块?

PHP数据库连接实践

swift - 为什么 if 语句会这样?

jquery根据菜单onchange值加载页面元素

c# - Delphi 中的 DataTable(如 DataSet)组件

c# - 如何防止终端(Windows-CE)在 5 分钟后关闭

loops - 在 Groovy 中获取下一个迭代器值

javascript - for循环在javascript中被跳过

java - 尝试使用java中的扫描仪读取回车键的敲击作为输入?