Reputation: 113
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
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");
Upvotes: 2
Views: 28953
Reputation:
You can try this, you can also add "else" statements for other key presses.
string key = "";
do
{
Console.Write("Enter Username: ");
username = Console.ReadLine();
Console.Write("Is this correct? (Y/N): ");
key = Console.ReadLine();
if (key.Equals("Y") | key.Equals("y"))
{
break;
}
} while (true);
Upvotes: 1
Reputation: 1
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");
}
Upvotes: 0
Reputation: 19482
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:
FormatException
msdn. So I've added a TryParse()
;string
to ConsoleKeyInfo
msdn - this makes the check for "Y" or "N" easier and I think clearer, and there is no need to cast the user input with ToLower() msdn and compare it with a string;Console.Write ( "\nVuoi continuare? (yes/no) " );
In general you should filter all data\info ( whatever ) comes from the user, to avoid exception.
Upvotes: 4
Reputation: 467
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)
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");
Upvotes: 2
Reputation: 19296
You should move code where you do operation to do while loop.
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");
}
Upvotes: 2
Reputation: 11577
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
Upvotes: 4
Reputation: 1148
You need to 'read' answer to be able to test it.
answer = Console.ReadLine();
Upvotes: 1