选项是/否 C# 控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17909008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Option yes/no C# Console
提问by Simbox97
I'm creating a C# program for the Currency Converter by console. At the end I would to insert "Continue? (yes/no)". Here the user have to chose. I've tried this but it doesn't work
我正在通过控制台为货币转换器创建一个 C# 程序。最后我会插入“继续?(是/否)”。这里用户必须选择。我试过这个,但它不起作用
float Dollaro = 1.32f, Euro;
float Cambio;
string EuroStr;
Console.Write("Euro: ");
EuroStr = Console.ReadLine();
Euro = float.Parse(EuroStr);
Cambio = Dollaro * Euro;
Console.WriteLine("Dollaro: " + Cambio);
Console.WriteLine("Vuoi continuare? (yes/no)");
Console.ReadLine();
string risposta = Console.ReadLine();
do
{
if (risposta.Equals("Y"))
{
Console.WriteLine("Yes");
break;
}
else if (risposta.Equals("N"))
{
Console.WriteLine("No");
break;
}
} while (risposta == "N");
回答by No Idea For Name
you want something like that
你想要这样的东西
float Dollaro = 1.32f, Euro;
float Cambio;
string EuroStr;
do
{
Console.Write("Euro: ");
EuroStr = Console.ReadLine();
Euro = float.Parse(EuroStr);
Cambio = Dollaro * Euro;
Console.WriteLine("Dollaro: " + Cambio);
Console.WriteLine("Vuoi continuare? (yes/no)");
Console.ReadLine();
string risposta = Console.ReadLine();
if (risposta.Equals("Y"))
{
Console.WriteLine("Yes");
break;
}
else if (risposta.Equals("N"))
{
Console.WriteLine("No");
break;
}
} while (risposta == "N");
but that's just a sample, you need to give more information so i'll give you better example. what does your code supposed to do? what other option does the user have? and so on
但这只是一个示例,你需要提供更多信息,所以我会给你更好的例子。你的代码应该做什么?用户还有什么其他选择?等等
回答by Rob van der Veer
You need to 'read' answer to be able to test it.
您需要“阅读”答案才能对其进行测试。
answer = Console.ReadLine();
回答by kmatyaszek
You should move code where you do operation to do while loop.
您应该将代码移动到您执行操作的地方执行 while 循环。
Try this:
尝试这个:
static void Main(string[] args)
{
float Dollaro = 1.32f, Euro;
float Cambio;
string EuroStr;
string risposta = "Y";
do
{
Console.Write("Euro: ");
EuroStr = Console.ReadLine();
Euro = float.Parse(EuroStr);
Cambio = Dollaro * Euro;
Console.WriteLine("Dollaro: " + Cambio);
Console.WriteLine("Vuoi continuare? (yes/no)");
risposta = Console.ReadLine();
if (risposta.Equals("Y"))
{
Console.WriteLine("Yes");
}
else if (risposta.Equals("N"))
{
Console.WriteLine("No");
}
} while (risposta == "Y");
}
回答by Simon
This should fix your problem: And you should make your variable "risposta" ToLower so that it doesnt matter if you type a small or big letter (y or Y)
这应该可以解决您的问题:并且您应该将变量设置为“risposta”ToLower,这样无论您输入小字母还是大字母(y 或 Y)都无关紧要
float Dollaro = 1.32f, Euro;
float Cambio;
string EuroStr;
string risposta = "Y";
do
{
Console.Write("Euro: ");
EuroStr = Console.ReadLine();
Euro = float.Parse(EuroStr);
Cambio = Dollaro * Euro;
Console.WriteLine("Dollaro: " + Cambio);
Console.WriteLine("Vuoi continuare? (yes/no)");
risposta = Console.ReadLine();
if (risposta.Equals("Y"))
{
Console.WriteLine("Yes");
}
else if (risposta.Equals("N"))
{
Console.WriteLine("No");
}
} while (risposta.ToLower() == "y");
回答by Bakudan
float Dollaro = 1.32f, Euro, Cambio;
string EuroStr;
ConsoleKeyInfo risposta;
do
{
Console.Write ( "Euro: " );
EuroStr = Console.ReadLine ();
bool result = Single.TryParse ( EuroStr, out Euro );
if ( result )
{
Cambio = Dollaro * Euro;
Console.WriteLine ( "Dollaro: " + Cambio );
} else {
Console.WriteLine ( "Please enter a number" );
}
bool check = false;
do {
Console.Write ( "\nVuoi continuare? (yes/no) " );
risposta = Console.ReadKey ( true );
check = !( ( risposta.Key == ConsoleKey.Y ) || ( risposta.Key == ConsoleKey.N ) );
} while ( check );
switch ( risposta.Key )
{
case ConsoleKey.Y: Console.WriteLine ( "Yes" ); break;
case ConsoleKey.N: Console.WriteLine ( "No" ); break;
}
} while ( risposta.Key != ConsoleKey.N );
I've changed some things:
我改变了一些东西:
- if I enter a characterfor the Euro -
FormatException
msdn. So I've added aTryParse()
; - I've changed the response from
string
toConsoleKeyInfo
msdn- this makes the check for "Y" or "N" easier and I think clearer, and there is noneed to cast the user input with ToLower() msdnand compare it with a string; - also a check if the user presses "Y" or "N", while the input is different, the same message will appear -
Console.Write ( "\nVuoi continuare? (yes/no) " );
- 如果我为 Euro- msdn输入一个字符。所以我添加了一个;
FormatException
TryParse()
- 我已经改变了从响应
string
到ConsoleKeyInfo
MSDN-这使得为“Y”或“N”的检查更容易,我想更清晰,有没有必要与ToLower将(投用户输入),MSDN,并将其与字符串比较; - 还要检查用户是否按下“Y”或“N”,当输入不同时,会出现相同的消息 -
Console.Write ( "\nVuoi continuare? (yes/no) " );
In general you should filter all data\info ( whatever ) comes from the user, to avoid exception.
通常,您应该过滤来自用户的所有数据\信息(任何内容),以避免出现异常。
回答by abz funnies
Try this code:
试试这个代码:
int num1, num2;
char oPt;
string Count;
do
{
Console.WriteLine("Enter 1st Value");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter 2nd Value : ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" + - * / ");
oPt = Convert.ToChar(Console.ReadLine());
if (oPt == '-')
{
Console.WriteLine("Result: " + (num1 - num2));
}
else if (oPt == '+')
{
Console.WriteLine("Result: " + (num1 + num2));
}
else if (oPt == '*')
{
Console.WriteLine("Result: " + (num1 * num2));
}
else if (oPt == '/')
{
Console.WriteLine("Result: " + (num1 / num2));
}
do
{
Console.WriteLine("Do you wish to calculate another? Yes (y) or No (n): ");
Count = Console.ReadLine();
var CountLower = Count?.ToLower();
if ((CountLower == "y") || (CountLower == "n"))
break;
} while (true );
} while (Count != "n");
}