【发布时间】: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