【问题标题】:C# - Tryinig to do exchange rates in if/else statement and pass argument into method parameters in main functionC# - Tryinig 在 if/else 语句中执行汇率并将参数传递给主函数中的方法参数
【发布时间】:2017-01-15 03:10:12
【问题描述】:

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

例如: 交换(不知道该放什么);

也不确定我是否正确完成了交换方法。该程序运行到询问用户要兑换什么货币的程度,但那只是当我注释掉兑换方法时。真的卡住了,有什么提示吗?

我想我可能必须为每一种货币 SEK、USD、EUR 赋值,但不知道从那里做什么......

任何帮助将不胜感激!

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

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

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!-------\n\n");

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

【问题讨论】:

  • main 函数的顶部没有打印在上面,但这是我不知道在 () 中写入什么的部分 - static void Main(string[] args) { writeMenu();交换(); ---- 这里---- Console.ReadKey(); }
  • 如果您只输入一键输入,我会在程序的第一部分查看 Console.ReadKey(true) - 更容易过滤掉不需要的按键,允许 ESC退出,并且不需要用户按 ENTER)
  • @ShannonHolsinger 我有 Console.ReadKey();在程序结束时去。当我请求他们想要转换多少钱时,用户希望输入多个密钥..

标签: c# methods arguments parameter-passing


【解决方案1】:

这里有一些想法可以让你继续前进……我觉得这是家庭作业,所以我只做了一些适度的重新工作。

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!-------\n\n");

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

【讨论】:

  • 谢谢你,Shannon,是的,这是我们课程的第一个作业。我喜欢你介绍开关和无效响应的方式。 “C2”代表什么?我不明白这部分..另外,我刚刚读到要使用小数,你必须有一个返回类型,所以谢谢你清理它。为什么必须将小数 newValue 设置为 0.000?
  • C2 告诉 ToString 将转换格式化为具有两位小数的货币字符串(C 表示货币 2 表示小数)。我只是设置了小数的原始值,以便对其进行签名。如果您不分配值,您将得到一个无符号异常,就像您对事物进行编码一样。正如我所说,我没有做太多的改变——这不是优化或概括的——只是给你一些想法。如果您觉得我对您有所帮助,请记住接受我的回答。祝你好运!
  • 好的,知道了。一旦你整天盯着屏幕看,从不同的角度来看是件好事。我如何接受你的回答?
  • 只需点击投票下方的复选标记即可获得我的答案。我很感激,并且完全理解僵尸综合症。保重!
猜你喜欢
  • 2016-03-02
  • 1970-01-01
  • 2018-01-21
  • 2019-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 2023-01-07
相关资源
最近更新 更多