I have a little problem at the beggining of my code. The code is the following:
do
{
Console.Write("x = ");
string x = Console.ReadLine();
double dx = Convert.ToDouble(x);
Console.Write("X must be bigger than 1.");
}
while (dx > 1);
I want my program to ask for x, until it's bigger than 1. The problem is, at the while part of the code, I'm getting this:
The name 'dx' doesn't exist in the current context. What should I do? Or the whole code is wrong?
You should create dx
outside loop, because variable is not visible outside { }
:
double dx;
do
{
Console.Write("x = ");
string x = Console.ReadLine();
dx = Convert.ToDouble(x);
Console.Write("X must be bigger than 1.");
}
while (dx > 1);
Also, you can refactor your code a little:
double dx;
do
{
Console.Write("x = ");
dx = Convert.ToDouble(Console.ReadLine()); //you can get exception here if your line can't be converted to double
Console.Write("X must be bigger than 1.");
}
while (dx > 1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.