Alternate Answer to Exercise 5 in Chapter 3
An alternative to the answer in the book that re-factors the code uses helper functions WriteNext and ConvertMyString:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exercise
{
class Program
{
static void Main(string[] args)
{
int firstValue, secondValue, thirdValue, fourthValue;
WriteNext ("1st");
firstValue = ConvertMyString();
WriteNext ("2nd");
secondValue = ConvertMyString();
WriteNext("3rd");
thirdValue = ConvertMyString();
WriteNext("4th");
fourthValue = ConvertMyString();
Console.WriteLine("The product of {0} {1} {2} and {3} is {4}.", firstValue, secondValue, thirdValue, fourthValue, firstValue * secondValue * thirdValue * fourthValue);
Console.ReadKey();
}
static int ConvertMyString()
{
int myInt;
myInt = Convert.ToInt32(Console.ReadLine());
return myInt;
}
static void WriteNext(string value)
{
Console.WriteLine("Enter {0} Value", value);
}
}
}
|