Hey all!
Got a quick question and a sneeky neenjuh feeling that I already have the answer.
For the excercise:
Quote:
|
2. Write an application that includes the logic from Exercise 1, obtains two numbers from the user, and displays them, but rejects any input where both numbers are greater than 10 and asks for two new numbers.
|
Now, I did this. But when I compared my code to the Appendix code - I was all

.... yea, I thunk I didded it wrong... Funny part? My code works.
So, answering my own question, there are different ways to code - just some is more efficient?
Their Code (example 2):
Code:
bool numbersOK = false;
double var1, var2;
var1 = 0;
var2 = 0;
while (!numbersOK)
{
Console.WriteLine("Give me a number:");
var1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Give me another number:");
var2 = Convert.ToDouble(Console.ReadLine());
if ((var1 > 10) && (var2 > 10))
{
Console.WriteLine("Only one number may be greater than 10.");
}
else
{
numbersOK = true;
}
}
Console.WriteLine("You entered {0} and {1}.", var1, var2);
My Code:
Code:
double firstNum, secNum;
Console.WriteLine("This program decides which one of 2 numbers is greater than 10.");
Console.WriteLine();
Console.WriteLine("What is your first number?");
firstNum = Convert.ToDouble(Console.ReadLine()); //defines firstNum
Console.WriteLine("What is your second number?");
secNum = Convert.ToDouble(Console.ReadLine()); //defines secNum
if ((firstNum > 10) && (secNum > 10)) //evaluates both numbers to be greater than
{
Console.WriteLine();
Console.WriteLine("Both of your numbers are greater than 10. Please try again.");
}
else if ((firstNum > 10) && (secNum < 10)) //evaluates firstNum to be greater
{
Console.WriteLine();
Console.WriteLine("Your first number {0} is greater than 10 and your second number {1} is less than 10.", firstNum, secNum);
}
else if ((firstNum < 10) && (secNum > 10)) // evaluates secNum to be greater
{
Console.WriteLine();
Console.WriteLine("Your first number {0} is less than 10 and your second number {1} is greater.", firstNum, secNum);
}
else // just in case both numbers are less than 10
{
Console.WriteLine("Both of your numbers are less than 10!");
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press any key to quit");
Console.ReadKey();
Obviously I could've used bool and some other techniques now seeing their code. I guess the shortcuts come with time? Or, did I miss an important lesson while taking notes?