如果条件,如何循环if语句与许多其他语句

时间:2015-07-09 14:46:14

标签: 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语句?

3 个答案:

答案 0 :(得分:5)

您只需要一次金属类型。移动两行,在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只留下一行。它也会重复,所需的值可以存储在Dictionary中。做到这一切,你最终可能得到一个解决方案:

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可能会被删除;你没有在解析之前测试权重输入是否是有效的双倍),但它会让你在正确的路径上。< / p>

答案 1 :(得分:3)

你必须在循环结束时重新分配用户选项,否则它永远不会改变:

while (!doesUserWantToLeave)
{
    if (conversionType == "Bronze")
    {
        //....
    }
    // ...
    else if (conversionType == "Exit")
    {
        doesUserWantToLeave = true;
    }
    else
    {
        Console.WriteLine("Sorry! That was an invalid option!");
    }
    conversionType = Console.ReadLine();
}

因此您还必须删除循环中的所有其他Console.ReadLine();。您也可以使用break代替离开循环的doesUserWantToLeave = true

答案 2 :(得分:-1)

您可以将这段代码提取为一个单独的方法,该方法将在主程序的循环中调用,请参阅下面的伪代码:

public void Main(string[] args)
{
   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();

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

      double weight = ConvertMethod(conversionType, waxWeight);

      Console.WriteLine(string.Format("You need {0} grams of {1}.", weight, conversionType));
   }
}

该方法如下所示。可以传递字符串,也可以使用枚举来定义转换类型。

public double ConvertMethod(string type, double weight)
{
   switch(type) {
      case "Silver":
         return weight * 10.5;
      case "Bronze":
         return weight * 10;

     // etc...
   }
}