c# -尝试在if/else语句中执行汇率,并在main函数中将参数传递给方法参数



我试图使用一种方法来计算汇率,然后将该参数传递给main中的方法。我不确定我是否已经正确地转换了钱,因为我无法弄清楚在main中调用方法时要把什么放入参数中。

例如

:交换(不知道在这里放什么);

也不确定我是否正确地使用了交换方法。程序一直运行到询问用户要兑换哪种货币,但只有当我注释掉兑换方法时才会这样。真的卡住了,有什么建议吗?

我想我可能必须为每一种货币分配价值瑞典克朗,美元,欧元,但不确定从那里做什么…

任何帮助都将非常感激!

(如果我在这个问题上输入了错误的代码,我很抱歉,不确定如何使它看起来更干净)

----这是一个控制台应用程序----

class Program
{
    static void Main(string[] args)
    {
        writeMenu();
        //exchange();
        //exchange(choiceFromCurrency, coiceToCurrency, valueToExchange);
        Console.ReadKey();
    }
    public static void writeMenu()
    {
        Console.WriteLine("Welcome to your next level Currency Converter!");
        Console.WriteLine("---We---Change---Your---Money---For---You---");
        Console.WriteLine(" -------So---You---Dont---Have---To!-------nn");
        Console.WriteLine("What is your base currency?n");
        Console.WriteLine("1 = SEK, 2= USD or 3= EUR?");
        string userInput = Console.ReadLine();
        if (userInput == "1")
        {
            Console.WriteLine("You have chosen SEK (Swedish Krona)n");
        }
        else if (userInput == "2")
        {
            Console.WriteLine("You have chosen USD (United States Dollar)n");
        }
        else
        {
            Console.WriteLine("You have chosen EUR (Euro)n");
        }
        Console.WriteLine("Which currency would you like to change your money to?n");
        string userInput2 = Console.ReadLine();
        if (userInput2 == "1")
        {
            Console.WriteLine("You have chosen SEK (Swedish Krona)n");
        }
        else if (userInput2 == "2")
        {
            Console.WriteLine("You have chosen USD (United States Dollar)n");
        }
        else
        {
            Console.WriteLine("You have chosen EUR (Euro)n");
        }
    }
    public static decimal exchange(decimal currencyToExchangeFrom, decimal currencyToExchangeTo )
    {
        Console.WriteLine("How much would you like to exchange?n");
        string amountToExchange = Console.ReadLine();
        decimal amountToConvert = 0;
        decimal.TryParse(amountToExchange, out amountToConvert);
        decimal newValue;
        // SEK
        if(currencyToExchangeFrom == 1)
        {
            // SEK - SEK
            if (currencyToExchangeTo == 1)
            {
                Console.WriteLine("You have your money, go spend it!");
            }
            // sek -usd
            if (currencyToExchangeTo == 2)
            {
                newValue = amountToConvert / 8.50m;
                Console.WriteLine("You now have" + newValue + " in USD");
            }
        }
        //sek - eur
        if(currencyToExchangeFrom == 2)
        {
            amountToConvert / 9.49m;
            Console.WriteLine("You now have" + newValue + " in EUR");
        }
        //  usd - eur
        if (currencyToExchangeFrom == 3)
        {
            amountToConvert * 0.90m;
            Console.WriteLine("You now have" + newValue + " in EUR");
        }

这里有一些让你开始的想法…我感觉这是作业,所以我只做了一些简单的修改。

static void Main(string[] args)
    {
        begin();
        Console.ReadLine();
    }
    public static void begin()
    {
        Console.WriteLine("Welcome to your next level Currency Converter!");
        Console.WriteLine("---We---Change---Your---Money---For---You---");
        Console.WriteLine(" -------So---You---Dont---Have---To!-------nn");
        Console.WriteLine("What is your base currency?n");
        Console.WriteLine("1 = SEK, 2= USD or 3= EUR?");
        ConsoleKeyInfo keyPress = Console.ReadKey(true);
        int uConvertFrom = getUserInput(keyPress);
        if (uConvertFrom > -1)
        {
            switch (uConvertFrom)
            {
                case 1:
                    Console.WriteLine("You have chosen SEK (Swedish Krona)n");
                    break;
                case 2:
                    Console.WriteLine("You have chosen USD (United States Dollar)n");
                    break;
                case 3:
                    Console.WriteLine("You have chosen EUR (Euro)n");
                    break;
            }
        }
        else
        {
            if (uConvertFrom == -2)
            {
                //break;
            }
            else
            {
                Console.WriteLine("You didn't enter a valid response. Please try again");
                begin();
            }
        }
        Console.WriteLine("Which currency would you like to change your money to?n");
        keyPress = Console.ReadKey(true);
        int uConvertTo = getUserInput(keyPress);
        if (uConvertTo > -1) {
            switch (uConvertTo)
            {
                case 1:
                    Console.WriteLine("You have chosen SEK (Swedish Krona)n");
                    break;
                case 2:
                    Console.WriteLine("You have chosen SEK (Swedish Krona)n");
                    break;
                case 3:
                    Console.WriteLine("You have chosen USD (United States Dollar)n");
                    break;
                case 4:
                    Console.WriteLine("You have chosen EUR (Euro)n");
                    break;
            }
        }
        else
        {
            if (uConvertFrom == -2)
            {
                //break;
            }
            else
            {
                Console.WriteLine("You didn't enter a valid response. Please try again");
                begin();
            }
        }
        exchange((decimal)uConvertFrom, (decimal)uConvertTo);
    }
    private static int getUserInput(ConsoleKeyInfo keyPress)
    {
        if (keyPress.Key == ConsoleKey.Escape)
        {
            Console.WriteLine("Thank you for using. Exiting now.");
            return -2;
        }
        int ret = -1;
        if (int.TryParse(keyPress.KeyChar.ToString(), out ret))
        {
            return ret;
        }
        else
        {
            return -1;
        }
    }
    public static decimal exchange(decimal currencyToExchangeFrom, decimal currencyToExchangeTo)
    {
        Console.WriteLine("How much would you like to exchange?n");
        string amountToExchange = Console.ReadLine();
        decimal amountToConvert = 0;
        decimal.TryParse(amountToExchange, out amountToConvert);
        decimal newValue = (decimal)0.000;
        // SEK
        if (currencyToExchangeFrom == 1)
        {
            // SEK - SEK
            if (currencyToExchangeTo == 1)
            {
                Console.WriteLine("You have your money, go spend it!");
            }
            // sek -usd
            if (currencyToExchangeTo == 2)
            {
                newValue = amountToConvert / 8.50m;
                Console.WriteLine("You now have" + newValue.ToString("C2") + " in USD");
            }
        }
        //sek - eur
        if (currencyToExchangeFrom == 2)
        {
            amountToConvert /= 9.49m;
            Console.WriteLine("You now have" + newValue.ToString("C2") + " in EUR");
        }
        //  usd - eur
        if (currencyToExchangeFrom == 3)
        {
            amountToConvert *= 0.90m;
            Console.WriteLine("You now have" + newValue.ToString("C2") + " in EUR");
        }
        return (decimal).001;
    }

相关内容

最新更新