|
-
October 17th, 2011, 07:55 PM
#1
Cant figure out simple console app
I have created a very simple console app that
1. Displays menu with list of options to choose
2. Should be able to add/substract/multiply/divide based on the option choosen
3. once option is chosen, should prompt the user to enter the inputs and provide the output.
Now i need to change to code in order to follow the below guidlines:
- Use one class per method.
- Common base class for all the operations.
Can someone please help me out with this?
The code is below:
using System;
namespace ConsoleApp
{
public class Choices
{
public static void Main()
{
int num1;
int num2;
string myChoice;
Choices chs = new Choices();
do
{
myChoice = chs.getChoice();
Console.Write("press Enter key to continue...");
Console.ReadLine();
Console.WriteLine();
// Make a decision based on the user's choice
switch (myChoice)
{
case "1":
Console.Write("Please enter the first integer: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the second integer: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write(num1);
Console.Write(" + ");
Console.Write(num2);
Console.Write(" = ");
Console.Write(num1 + num2);
break;
case "2":
Console.Write("Please enter the first integer: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the second integer: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write(num1);
Console.Write(" - ");
Console.Write(num2);
Console.Write(" = ");
Console.Write(num1 - num2);
break;
case "3":
Console.Write("Please enter the first integer: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the second integer: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write(num1);
Console.Write(" / ");
Console.Write(num2);
Console.Write(" = ");
Console.Write(num1 / num2);
break;
case "4":
Console.Write("Please enter the first integer: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the second integer: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write(num1);
Console.Write(" * ");
Console.Write(num2);
Console.Write(" = ");
Console.Write(num1 * num2);
break;
case "5":
Console.WriteLine("Bye.");
break;
default:
Console.WriteLine("{0} is not a valid choice", myChoice);
break;
}
Console.ReadLine();
Console.ReadLine();
Console.WriteLine();
} while (myChoice != "5"); // Keep going until the user wants to quit
}
string myChoice;
string getChoice()
{
// Print A Menu
Console.WriteLine("What would you like to do?");
Console.WriteLine("1 - Do you want to add two numbers?");
Console.WriteLine("2 - Do you want to subtract two numbers?");
Console.WriteLine("3 - Do you want to divide two numbers?");
Console.WriteLine("4 - Do you want to multiply two numbers?");
Console.WriteLine("5 - Quit\n");
Console.Write("Choice (1,2,3,4,or 5): ");
// Retrieve the user's choice
myChoice = Console.ReadLine();
Console.WriteLine();
return myChoice;
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|