我的循环不起作用 - 它应该在它之前退出

时间:2014-05-07 23:41:15

标签: c# loops

我是编程新手。我尝试了这个代码,循环不起作用。控制台应用程序只在一次操作后结束,我想继续做更多,直到我选择说“不”。我在哪里弄错了?我希望程序能继续下去,直到我说出来。提前谢谢。

    static void Main(string[] args)
    {
        Console.WriteLine("Program to convert USA Dollars to five different currencies,");
        Console.WriteLine();
        double dollars, euros, turkisLira, yen, britishPounds, mexicanPeso;
        char option;
        euros = 0.72;
        turkisLira = 2.09;
        yen = 101.73;
        britishPounds = 0.59;
        mexicanPeso = 13.03;

        string answer = "Y";

        do
        {

            Console.Write("Enter the Amount of USA Dollars to Convert:");
            dollars = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("1.Convert to Euros");
            Console.WriteLine("2.Convert to Turkis Lira");
            Console.WriteLine("3.Convert to Japanes Yen");
            Console.WriteLine("4.Convert to British Pounds");
            Console.WriteLine("5.Convert to Mexican Pesos");
            Console.Write("Enter the option of Currency you wish to convert : ");

            option = Convert.ToChar(Console.ReadLine());
            switch (option)
            {
                case '1':
                    euros = dollars * euros;
                    Console.WriteLine("The amount of Euros given is : {0}", euros);
                    break;
                case '2':
                    turkisLira = dollars * turkisLira;
                    Console.WriteLine("The amount of Turkis Liras given is : {0}", turkisLira);
                    break;
                case '3':
                    yen = dollars * yen;
                    Console.WriteLine("The amount of Japanes Yen given is : {0}", yen);
                    break;
                case '4':
                    britishPounds = dollars * britishPounds;
                    Console.WriteLine("The amount of British Pounds given is : {0}", britishPounds);
                    break;
                case '5':
                    mexicanPeso = dollars * mexicanPeso;
                    Console.WriteLine("The amount of Mexican Pesos given is : {0}", mexicanPeso);
                    break;

            }
            Console.WriteLine(" Do you wish to do more conversions? (yes/no)");
            answer = Console.ReadLine();

            if (answer.Equals("Y"))
            {
                Console.WriteLine("Yes");
            }
            else if (answer.Equals("N"))
            {
                Console.WriteLine("No");
            }
        } while (answer.ToLower() == "y'");

2 个答案:

答案 0 :(得分:0)

您的要求是

  

我想继续做更多事情,直到我选择拒绝

而你现有的条件是

(answer.ToLower() == "y'");

所以,如果有人点击y以外的任何内容,循环就会退出。您希望存在条件

(answer.ToLower() != "n'");-

答案 1 :(得分:0)

我认为你错过了类型'在" y'"你想要收到的地方" y"继续。