首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用许多其他if条件循环if-语句

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

Stack Overflow用户
提问于 2015-07-09 14:46:14
回答 3查看 232关注 0票数 6

我在代码中循环if语句时遇到了问题。我查看了堆栈溢出上的其他线程,但无法让它多次工作。我试图创建的程序是一家铸造公司的基本转换器。我想做的是让用户输入所需的转换类型,然后输入蜡的重量。然后,它将给用户正确的量的贵金属使用。问题是,我需要从一开始就运行它,直到用户使用它为止。我尝试使用while语句,但它只是循环if-语句的其他部分。这是我的代码供参考:

代码语言:javascript
复制
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语句吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-09 14:51:04

你只要求金属类型一次。将提示输入和接收用户输入的两行移动到while循环中:

代码语言:javascript
复制
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之后重复下列三行,因此只需要在循环的顶部重复一次:

代码语言:javascript
复制
Console.WriteLine("What is the weight of the wax model?");
wW = Console.ReadLine();
waxWeight = double.Parse(wW);

接下来,每个if中的最后两行基本重复,但唯一更改的部分(金属名称)是已知的。因此,它们都可以被删除并替换为循环末尾的一个副本:

代码语言:javascript
复制
Console.WriteLine("You need " + metalWeight + " grams of {0}.", 
                  conversionType.ToLower());
Console.ReadLine();

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

代码语言:javascript
复制
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可能会被删除;在解析之前,您没有测试权重输入是否是有效的双倍),但它将使您走上正确的道路。

票数 5
EN

Stack Overflow用户

发布于 2015-07-09 14:50:34

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

代码语言:javascript
复制
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

票数 3
EN

Stack Overflow用户

发布于 2015-07-09 14:48:17

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

代码语言:javascript
复制
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));
   }
}

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

代码语言:javascript
复制
public double ConvertMethod(string type, double weight)
{
   switch(type) {
      case "Silver":
         return weight * 10.5;
      case "Bronze":
         return weight * 10;

     // etc...
   }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31320907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档