2016-09-07 47 views
0

我试图使用一种方法来计算汇率,然后将该参数传回给main中的方法。我不确定我是否已经正确地转换了钱,因为我无法弄清楚在主要调用方法时要将参数放入参数中。C#Tryinig在if/else语句中执行汇率并将参数传递到主函数中的方法参数

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

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

我想我可能要值分配给每个货币瑞典克朗,美元,欧元,但不知道从那里做...

任何帮助将不胜感激!

(如果我enetered我的代码错在这个问题上我很抱歉,真的不知道如何使它看起来任何清洁剂)

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

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

主要功能didnt打印上面的顶部但是这是我不知道什么写入()的部分 - 静态无效的主要(字符串[]参数)) { writeMenu(; exchange(); ----这里---- Console.ReadKey(); } – molebox

+0

如果您只需要一键录入,我会在程序的第一部分查看Console.ReadKey(true) - 更容易过滤掉不需要的按键,允许ESC退出,并且不会' t要求用户按ENTER) –

+0

@ShannonHolsinger我有Console.ReadKey();在节目结束时进行。当我要求他们想要转换多少钱时,用户将希望输入多个键。 – molebox

回答

0

这里有一些想法,让你去...我有一种感觉,这是作业,所以我只做了一些适度的重新工作。

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

谢谢香农,是的,这是我们课程的第一项任务。我喜欢你如何介绍交换机和无效响应。 “C2”代表什么?我不明白这一部分..也就是我读了,要使用小数,你必须有一个返回类型,所以谢谢你清理。为什么你必须将十进制的newValue设置为0.000? – molebox

+0

C2告诉ToString将转换格式转换为带有两位小数的货币字符串(C表示小数点后的货币2)。我只是设置小数的原始值,所以它会被签名。如果你没有赋值,你会得到一个无符号的异常,就像编码的东西一样。正如我所说的,我没有做太多的改变 - 这并没有被优化或推广 - 只是给你一些想法。如果您觉得我的帮助很大,请记住接受我的回答。祝你好运! –

+0

好吧。一旦你一整天都在盯着屏幕,从不同的角度看它是很好的。我如何接受你的回答? – molebox