G
Guest
Complete n00b here, so please bear with me.
I have written the following code and would like to know if it could be
optimised. In particular, I'm reading the user input for the operand into
variable strOperand, and rather than use the IF statement to determine which
operand to use, I'd like to use the variable contents
e.g. dblAnswer = dblFirstNo strOperand dblSecondNo
Also, the While clause at the end...ideally, I would like to write something
like
while (strMore != "y" and strMore !="n")
but I don't know how to do this.
Any help appreciated.
Code ************************************
static void Main(string[] args)
{
//declare the variables to be used with default values
double dblFirstNo = 0, dblSecondNo = 0, dblAnswer = 0;
string strMore = "y", strOperand = "*";
//keep going until the user enters "n"
while (strMore == "y")
{
//get the first number
System.Console.WriteLine("Please enter a number ");
dblFirstNo =
System.Convert.ToDouble(System.Console.ReadLine());
//ask what they want to do with the numbers
System.Console.WriteLine
("Please enter an operand ( * / + -
)");
strOperand =
System.Console.ReadLine();
//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo =
System.Convert.ToDouble(System.Console.ReadLine());
//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
//output the sum and the answer
System.Console.WriteLine
("{0} * {1} =
{2}",dblFirstNo,dblSecondNo,dblAnswer);
//ask if the user wants to continue
strMore = "z";
while (strMore == "z")
{
System.Console.WriteLine
("Do you want to
enter more? (y/n)");
strMore = System.Console.ReadLine();
//check to see what the user entered
//was it Y
if (strMore != "y")
{
//nope was it n
if (strMore != "n")
{
//set it to z so the
//system can loop
//ideal...don't think so,
//but it'll do until
//I get more proficient
strMore = "z";
}
}
}
}
}
I have written the following code and would like to know if it could be
optimised. In particular, I'm reading the user input for the operand into
variable strOperand, and rather than use the IF statement to determine which
operand to use, I'd like to use the variable contents
e.g. dblAnswer = dblFirstNo strOperand dblSecondNo
Also, the While clause at the end...ideally, I would like to write something
like
while (strMore != "y" and strMore !="n")
but I don't know how to do this.
Any help appreciated.
Code ************************************
static void Main(string[] args)
{
//declare the variables to be used with default values
double dblFirstNo = 0, dblSecondNo = 0, dblAnswer = 0;
string strMore = "y", strOperand = "*";
//keep going until the user enters "n"
while (strMore == "y")
{
//get the first number
System.Console.WriteLine("Please enter a number ");
dblFirstNo =
System.Convert.ToDouble(System.Console.ReadLine());
//ask what they want to do with the numbers
System.Console.WriteLine
("Please enter an operand ( * / + -
)");
strOperand =
System.Console.ReadLine();
//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo =
System.Convert.ToDouble(System.Console.ReadLine());
//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
//output the sum and the answer
System.Console.WriteLine
("{0} * {1} =
{2}",dblFirstNo,dblSecondNo,dblAnswer);
//ask if the user wants to continue
strMore = "z";
while (strMore == "z")
{
System.Console.WriteLine
("Do you want to
enter more? (y/n)");
strMore = System.Console.ReadLine();
//check to see what the user entered
//was it Y
if (strMore != "y")
{
//nope was it n
if (strMore != "n")
{
//set it to z so the
//system can loop
//ideal...don't think so,
//but it'll do until
//I get more proficient
strMore = "z";
}
}
}
}
}