How to re-write menu using DRY - c#

I'm doing a school project and I really feel that I can acomplish what I want in my code, however I feel like i -always- repeat my code.
I have tried to put the switch cases into methods but haven't had any success though to out of scope.
I am beginner so sorry for the eyesore code!
However, if anyone have any tips for using methods or something else so I can clean up my code a little better.
So here is a bit of my code, this is the beginning branch of my menu.
I have translated the code roughly to english.
So this is a sub menu in the main method.
string[] flowers = { "Sunflower", "Coleus Tree", "Tomato" };
int[] plant = { 0, 0, 0 };
switch (menuInput03)
{
/////WATER PLANT////
case 1:
Console.Clear();
Console.WriteLine("Press SPACEBAR several times to water plant!");
if (plant[0] < 21)
{
for (int i = plant[0]; i < 21; i++)
{
var keyPressed = Console.ReadKey();
if (keyPressed.Key == ConsoleKey.Spacebar)
{
plant[0] = i;
Console.Clear();
Console.WriteLine("Press SPACEBAR several times to water plant!");
Console.WriteLine("\nWatering plant..");
Console.WriteLine("[" + flowers[0] + " " + plant[0] + "/100]");
}
}
while (true)
{
Console.Clear();
Console.WriteLine("Your plant has been watered!");
Console.WriteLine("[" + flowers[0] + " " + plant[0] + "/100]\n");
Console.WriteLine("To continue press ENTER.");
var keyPressed01 = Console.ReadKey();
if (keyPressed01.Key == ConsoleKey.Enter)
{
Console.Clear();
break;
}
else
{
Console.Clear();
}
}
}
else
{
break;
}
Console.Clear();
break;
case 2:
Console.Clear();
Console.WriteLine("Press SPACEBAR several times to remove weed!");
if (plant[0] > 19)
{
for (int i = plant[0]; i < 51; i++)
{
var keyPressed = Console.ReadKey();
if (keyPressed.Key == ConsoleKey.Spacebar)
{
plant[0] = i;
i++;
Console.Clear();
Console.WriteLine("Press SPACEBAR several times to remove weed!");
Console.WriteLine("\nRipping out weeds..");
Console.WriteLine("[" + flowers[0] + " " + plant[0] + "/100]");
}
}
while (true)
{
Console.Clear();
Console.WriteLine("Your plant has gotten it's weed removed!");
Console.WriteLine("[" + flowers[0] + " " + plant[0] + "/100]\n");
Console.WriteLine("To continue press ENTER.");
var keyPressed01 = Console.ReadKey();
if (keyPressed01.Key == ConsoleKey.Enter)
{
Console.Clear();
break;
}
else
{
Console.Clear();
}
}
}
Console.Clear();
break;`

I came up with the following; don't just copy-paste for school project without understanding what is going on. If some things are not clear do not hesitate to ask follow-up questions.
public record Action
{
public string Title { get; }
public int RequiredHealth { get; }
public string ActionMessage { get; }
public string CompletedMessage { get; }
private Action(string title, int requiredHealth, string actionMessage, string completedMessage)
{
Title = title;
RequiredHealth = requiredHealth;
ActionMessage = actionMessage;
CompletedMessage = completedMessage;
}
public static Action WaterPlant { get; } = new Action(
title: "Press SPACEBAR several times to water plant!",
requiredHealth: 20,
actionMessage: "Watering plant..",
completedMessage: "Your plant has been watered!");
public static Action RemoveWeed { get; } = new Action(
title: "Press SPACEBAR several times to remove weed!",
requiredHealth: 50,
actionMessage: "Ripping out weeds..",
completedMessage: "Your plant has gotten it's weed removed!");
}
static void Main()
{
string[] flowers = { "Sunflower", "Coleus Tree", "Tomato" };
int[] plant = { 0, 0, 0 };
for (var menuInput03 = 1; menuInput03 <= 2; menuInput03++)
{
var action = menuInput03 switch
{
1 => Action.WaterPlant,
2 => Action.RemoveWeed,
};
Console.Clear();
Console.WriteLine(action.Title);
while (plant[0] < action.RequiredHealth)
{
if (Console.ReadKey().Key != ConsoleKey.Spacebar)
{
Console.Write("\b \b");
continue;
}
plant[0]++;
Console.Clear();
Console.WriteLine(action.Title);
Console.WriteLine(action.ActionMessage);
Console.WriteLine($"[{flowers[0]} {plant[0]}/100]");
}
Console.Clear();
Console.WriteLine(action.CompletedMessage);
Console.WriteLine($"[{flowers[0]} {plant[0]}/100]");
Console.WriteLine("To continue press ENTER.");
while (Console.ReadKey().Key != ConsoleKey.Enter)
{
Console.Write("\b \b");
}
Console.Clear();
}
}

Related

Choosing in menu wirh arrows on keyboard (C#)

I am trying to make a simple menu where I can choose with the arrows on the keyboard. I can run the code and the program doesn't give any errors, however I can not choose with the arrows and nothing happens if I press Enter, the choice always remains on the first choice (New customer). That will say, It always stays like this:
Hello and welcome! please choose type of registration:
*New customer <--
New staff
Service
Reparation
Garantie
My code this far is:
using System;
namespace uppdrag_2.cs
{
class Program
{
static void Main(string[] args)
{
string[] menuOptions = new string[] {"New customer\t", "New staff\t", "Serivce\t", "Reparation", "Garantie" };
int menuSelect = 0;
while (true)
{
Console.Clear();
Console.CursorVisible = false;
Console.WriteLine("Hello and welcome! Please choose type of registration:");
if (menuSelect == 0)
{
Console.WriteLine("* " + menuOptions[0] + "<--");
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.ReadLine();
}
else if (menuSelect == 1)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine("* " + menuOptions[1] + "<--");
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.ReadLine();
}
else if (menuSelect == 2)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine("* " + menuOptions[2] + "<--");
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.ReadLine();
}
else if (menuSelect == 3)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine("* " + menuOptions[3] + "<--");
Console.WriteLine(menuOptions[4]);
Console.ReadLine();
}
else if (menuSelect == 4)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine("* " + menuOptions[4] + "<--");
Console.ReadLine();
}
var keyPressed = Console.ReadKey();
if (keyPressed.Key == ConsoleKey.DownArrow && menuSelect != menuOptions.Length - 1)
{
menuSelect++;
}
else if (keyPressed.Key == ConsoleKey.UpArrow && menuSelect >= 1)
{
menuSelect--;
}
else if (keyPressed.Key == ConsoleKey.Enter)
{
switch (menuSelect)
{
case 0:
Newcustomer();
break;
case 1:
NewStaff();
break;
case 2:
Service();
break;
case 3:
Reparation();
break;
case 4:
Garantie();
break;
}
}
}
}
public static void Newcustomer(){
Console.WriteLine("You have chosen to registrate a new customer!");
Console.WriteLine("Please enter name of customer:");
string name = Console.ReadLine();
Console.WriteLine("Enter car brand:");
string carBrand = Console.ReadLine();
Console.WriteLine("Enter model of car");
string model = Console.ReadLine();
Console.WriteLine("Enter year model");
string yearModel = Console.ReadLine();
Console.WriteLine("Enter how many km the car has been driven");
string km = Console.ReadLine();
Console.WriteLine("Registration compleated!");
Console.WriteLine("You have registered a new customer! You have registrered" + " " + name + " " + "with the carbrand" + carBrand + " " + "of model" + " " + model + " " + "form year" + " " + yearModel + " " + "That has been driven" + " " + km + "km.");
Console.ReadKey();
}
public static void NewStaff(){
Console.WriteLine("You have chosen to registrer a new staffmember!");
Console.WriteLine("Please enter name of staffmember:");
string staffName = Console.ReadLine();
Console.WriteLine("Enter age:");
string ageStaff = Console.ReadLine();
Console.WriteLine("Enter work position of the new staffmember:");
string position = Console.ReadLine();
Console.WriteLine("Enter type of contract:");
string contract = Console.ReadLine();
Console.WriteLine("Enter date of the workers first day:");
string start = Console.ReadLine();
Console.WriteLine("Enter date of the workrs last day:");
string end = Console.ReadLine();
Console.WriteLine("Registration compleated!");
Console.WriteLine("You have registrered a new staffmember! The following information has been registered:\t" + "name: " + staffName + "\t" + "age: " + ageStaff + "\t" + "position: " + position + "\t" + "Contract: " + contract + "\t" + "First day: " + start + "\t" + "Last day: " + end);
Console.ReadKey();
}
public static void Service(){
Console.WriteLine("Please enter type of service:");
string service = Console.ReadLine();
Console.WriteLine("Enter the name of an registered customer. If customer is not already registered, please press Enter to return to menu and choose `New customer`");
string customerservice = Console.ReadLine();
Console.WriteLine("Enter staffmember responsible for this matter. If staffmember is new, please press Enter to return to the menu and choose ``New staff`");
string servicestaff = Console.ReadLine();
Console.WriteLine("Enter staring day of service:");
string serviceStart = Console.ReadLine();
Console.WriteLine("Enter deadline of servicematter:");
string deadline = Console.ReadLine();
Console.WriteLine("Service matter registrered!");
Console.WriteLine("Following information has been registrered:\t" + "Service matter: " + service + "\t" + "Customer: " + customerservice + "\t" + "Staff member responsible: " + servicestaff + "\t" + "Startdate of service matter: " + serviceStart + "\t" + "Deadline: " + deadline);
Console.ReadKey();
}
public static void Reparation(){
Console.WriteLine("You have choosen to registrer a reparation matter!");
Console.ReadLine();
Console.ReadKey();
}
public static void Garantie(){
Console.WriteLine("You have choosen to registrate a garantie matter!");
Console.WriteLine();
Console.ReadKey();
}
}
}
Anyone who could help me figure out what I`ve done wrong?
You can shorten your menu code quite a bit by using a for loop and a ternary if statement:
static void Main(string[] args)
{
string[] menuOptions = new string[] { "New customer\t", "New staff\t", "Serivce\t", "Reparation\t", "Garantie\t" };
int menuSelect = 0;
while (true)
{
Console.Clear();
Console.CursorVisible = false;
Console.WriteLine("Hello and welcome! Please choose type of registration:");
for (int i = 0; i < menuOptions.Length; i++)
{
Console.WriteLine((i == menuSelect ? "* " : "") + menuOptions[i] + (i == menuSelect ? "<--" : ""));
}
var keyPressed = Console.ReadKey();
if (keyPressed.Key == ConsoleKey.DownArrow && menuSelect != menuOptions.Length - 1)
{
menuSelect++;
}
else if (keyPressed.Key == ConsoleKey.UpArrow && menuSelect >= 1)
{
menuSelect--;
}
else if (keyPressed.Key == ConsoleKey.Enter)
{
switch (menuSelect)
{
case 0:
Newcustomer();
break;
case 1:
NewStaff();
break;
case 2:
Service();
break;
case 3:
Reparation();
break;
case 4:
Garantie();
break;
}
}
}
}
public static void Newcustomer()
{
Console.WriteLine("New Customer ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
public static void NewStaff()
{
Console.WriteLine("New Staff ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
public static void Service()
{
Console.WriteLine("Service ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
public static void Reparation()
{
Console.WriteLine("Reparation ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
public static void Garantie()
{
Console.WriteLine("Garantie ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
}
It works fine for me:

Countdown timer with user input to set time c#

I am trying to build a countdown timer that if the selected choice is set time, it will open and prompts the user to enter value to set the time. once the user is done in typing the numbers, it must show choices like this
Press S to start: Press P to pause
This is what I am lacking, I do not know what to code to enable to pause the time and to start it.
using System;
namespace time_console
{
class Program
{
static void Main(string[] args)
{
designNumbers Fancy = new designNumbers();
Program mainMenu = new Program();
mainMenu.RunMainMenu();
}
private void RunMainMenu()
{
string prompt = "Welcome to Neil's Countdown Timer. Select an option";
string[] options = { "Set Time", "Exit" };
Menu mainMenu = new Menu(prompt, options);
int selectedIndex = mainMenu.Run();
switch (selectedIndex)
{
case 0:
SetTime();
break;
case 1:
Exit();
break;
}
}
private void SetTime()
{
Console.SetCursorPosition(4, 4);
Console.WriteLine("enter time in minutes:seconds ");
Console.SetCursorPosition(4, 5);
int m = Convert.ToInt32(Console.ReadLine());
Console.SetCursorPosition(4, 6);
int s = Convert.ToInt32(Console.ReadLine());
for (; ; )
{
if (s == 0 && m == 0)
{
Console.WriteLine("\nTime's up!");break;
}
if (s == 0)
{
s = 60;
m--;
}
System.Console.Clear();
Console.SetCursorPosition(4, 7);
Console.WriteLine(m + ":" + s--);
System.Threading.Thread.Sleep(1000);
}
WriteLine("Press M to return to Menu");
ConsoleKey a;
ConsoleKeyInfo b = ReadKey(true);
a = b.Key;
if(a == ConsoleKey.M)
{
RunMainMenu();
}
}
private void Exit()
{
WriteLine("press any key to exit...");
ReadKey(true);
Environment.Exit(0);
}
}
}

How to get out of a while-loop in c#

Here I am trying to have the user enter "YES" to continue with the game but i'm stuck in the while-loop. This is also my first post so I don't the correct way to post ie. copy and paste? Also is there a indentation shortcut for Visual Studio? Thanks for helping.
using System;
namespace BIT_142_Major_Assignment_1
{
class Program
{
static void Main(string[] args)
{
int numGames = 0;
String answer = "";
Random rand = new Random();
int Dice1 = rand.Next(1, 7);
int Dice2 = rand.Next(1, 7);
Console.WriteLine("Hey! Welcome to Andy’s Dice Game.");
Console.WriteLine("Let’s start!");
{
while (true)
{
numGames++;
Console.WriteLine("Dice 1: " + Dice1);
Console.WriteLine("Dice 2: " + Dice2);
Console.WriteLine("I got " + Dice1 + " and " + Dice2 + "!");
if ((Dice1 + Dice2) % 2 == 0)
{
Console.WriteLine("Evens are better than odds!");
}
else
{
Console.WriteLine("Odds are still cool!");
Console.WriteLine("Do you want to play again?");
answer = Console.ReadLine();
if (answer == "YES")
{
continue;
}
else
{
Console.WriteLine("The number of times the dice was thrown is: " + numGames);
Console.WriteLine("Nice Game!");
Console.WriteLine("Thank for playing, Come play again soon!");
break;
}
}
}
}
}
}
}

How do I jump into another menu without nesting unnecessarily?

I'm stuck with the following code and what I'm trying to achieve now, is to NOT nest switches/if statements any further, which would result in ugly code, I assume. To visualize my intention:
ConsoleKeyInfo keyE = Console.ReadKey();
if (keyE.Key == ConsoleKey.Enter && iMOP == 1){
//Jump to character creation (but not nest this if statement even further)
{
I want to apply this idea to a solved problem which I have already asked before:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.CursorVisible = false;
int iMOP = 1;
Console.WriteLine(" >>New Game");
Console.WriteLine(" Load Game");
Console.WriteLine(" Exit Game");
while (iMOP != 5)
{
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.UpArrow)
{
iMOP--;
}
else if (keyInfo.Key == ConsoleKey.DownArrow)
{
iMOP++;
}
}
if (iMOP == 0)
{
iMOP = 3;
}
else if (iMOP == 4)
{
iMOP = 1;
}
switch (iMOP)
{
case 1:
Console.Clear();
Console.WriteLine(" >>New Game");
Console.WriteLine(" Load Game");
Console.WriteLine(" Exit Game");
break;
case 2:
Console.Clear();
Console.WriteLine(" New Game");
Console.WriteLine(" >>Load Game");
Console.WriteLine(" Exit Game");
break;
case 3:
Console.Clear();
Console.WriteLine(" New Game");
Console.WriteLine(" Load Game");
Console.WriteLine(" >>Exit Game");
break;
}
}
}
}
The questions would be: How do I realize this? Are there decent tutorials which you could link me to?
If it is going to get too complicated, use a Console based Graphics engine (Is there any console "graphics" library for .Net?).
MonoCurses seem to be very friendly: http://www.mono-project.com/docs/tools+libraries/libraries/monocurses/
But, just for curiosity, something much simpler (from scratch):
Use Classes to Create, Display and Get the user input for your menu.
How it works
It is basically a Menu Class, that displays its MenuItem classes, and draw while wait the user select a option.
This menu class was then inserted in a nested Switch, for the Main Menu levels.
I tried to do something better, needs refactoring, so you can have some idea to improve:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication22
{
class Program
{
static void Main(string[] args)
{
do
{
#region Main Menu
var m = new Menu() { Title = "MAIN MENU" };
m.Menus.Add(new MenuItem() { Title = "New Game", SelectedForegroundColor = ConsoleColor.Green });
m.Menus.Add(new MenuItem() { Title = "Load Game" });
m.Menus.Add(new MenuItem() { Title = "Exit Game", SelectedForegroundColor = ConsoleColor.Yellow });
var result = m.ShowAndWaitUserInput();
//Console.Write("selected was:" + result);
//Console.ReadLine();
#endregion
switch (result)
{
case 0:
#region // Jump to character creation
var m1 = new Menu() { Title = "CHARACTER CREATION" };
m1.Menus.Add(new MenuItem() { Title = "New Character" });
m1.Menus.Add(new MenuItem() { Title = "Load Character" });
m1.Menus.Add(new MenuItem() { Title = "Return" });
var result1 = m1.ShowAndWaitUserInput();
switch (result1)
{
case 0:
#region // CREATE THE NEW CHARACTER
Console.WriteLine("CREATE THE NEW CHARACTER");
Console.ReadKey();
#endregion
break;
case 1:
#region // LOAD CHARACTER
Console.WriteLine("LOAD CHARACTER");
Console.ReadKey();
// Write your code here...
#endregion
break;
case 2:
#region // DO ALMOST NOTHING: RETURNS TO PREVIOUS MENU
#endregion
break;
}
#endregion
break;
case 1:
#region // Load Game code...
Console.WriteLine("LOAD GAME");
Console.ReadKey();
// Write your code here...
#endregion
break;
case 2:
#region // EXIT GAME
#endregion
return;
}
} while (true); // Main Menu
}
}
public class MenuItem
{
public string Title { get; set; }
public ConsoleColor SelectedForegroundColor { get; set; }
public MenuItem()
{
this.SelectedForegroundColor = ConsoleColor.Yellow;
}
}
public class Menu
{
public string Title { get; set; }
public List<MenuItem> Menus { get; set; }
public Menu()
{
this.Menus = new List<MenuItem>();
}
public int ShowAndWaitUserInput()
{
Console.CursorVisible = false;
int selectedMenu = 0;
do
{
Console.Clear();
#region Display Menu and Sub-Menus
{
var i = 0;
// more at http://www.graphics.cornell.edu/~westin/misc/windows_charmap.html
Console.WriteLine(" ╔═══════════════════════════════════════════╗");
Console.WriteLine(" ║ ░ " + this.Title.ToUpper().PadRight(40).Substring(0, 40) + "║");
Console.WriteLine(" ╚═══════════════════════════════════════════╝");
Console.WriteLine("");
//var old_CB = Console.BackgroundColor;
var old_CF = Console.ForegroundColor;
foreach (var menu in Menus)
{
//Console.WriteLine(" New Game");
if (i == selectedMenu)
{
Console.ForegroundColor = menu.SelectedForegroundColor;
Console.Write(" >> ");
}
else
{
Console.ForegroundColor = old_CF;
Console.Write(" ");
}
Console.WriteLine(menu.Title);
i++;
}
Console.WriteLine("");
//Console.BackgroundColor = old_CB;
Console.ForegroundColor = old_CF;
}
#endregion
#region Get User Input
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.UpArrow)
{
selectedMenu--;
if (selectedMenu < 0) selectedMenu = Menus.Count() - 1;
}
else if (keyInfo.Key == ConsoleKey.DownArrow)
{
selectedMenu++;
if (selectedMenu == Menus.Count()) selectedMenu = 0;
}
else if (keyInfo.Key == ConsoleKey.Enter)
{
return selectedMenu;
}
}
#endregion
} while (true);
}
}
}

How to loop this?

Im having trouble learning how to loop, and Im stuck on how to do this. Basically I was asked to program that rolls a 6 sided die and it'll ask you how many times you want to roll it. Based on how many times you roll, it will out put a table of how many times it landed on each side. This is what I have so far.
using System;
namespace Dice
{
class Program
{
static void Main(string[] args)
{
bool continueRunning = true;
int sessionNumber = 1;
DisplayInstructions();
while (continueRunning)
{
int howMany = int.Parse(getInfo("How many times do you want to roll the die?"));
Dice aDice = new Dice();
aDice.RollDice();
Console.Clear();
Console.WriteLine("Session Number: {0}", sessionNumber);
Console.WriteLine(aDice);
continueRunning = getYorN("Would you like to run again?");
sessionNumber++;
Console.Clear();
}
}
public static bool getYorN(string question)
{
bool validInput = false;
while (!validInput)
{
Console.WriteLine("{0}", question);
Console.WriteLine("Enter 'yes' or 'no' to continue...");
string userResponse = Console.ReadLine().ToLower();
if (userResponse == "yes" || userResponse == "no")
{
validInput = true;
switch (userResponse)
{
case "yes":
return true;
case "no":
return false;
default:
return false;
}
}
else
{
Console.Clear();
Console.WriteLine("You've entered an invalid term");
}
}
return false;
}
public static void DisplayInstructions()
{
Console.WriteLine("Welcome to the Dice Game!!");
Console.WriteLine("\n\n\nThis program will simulate rolling a die and will track the frequency \neach value is rolled.");
Console.WriteLine("\n\n\nAfter rolling the die, the program will output a summary table for the session.");
Console.WriteLine("\n\n\nPlease press any key to continue.");
Console.ReadKey();
Console.Clear();
}
public static string getInfo(string what)
{
Console.WriteLine(what);
return Console.ReadLine();
}
}
}
The class I have in this is
using System;
namespace TripCalcApp
{
class Dice
{
private int side1 = 0, side2 = 0, side3 = 0, side4 = 0, side5 = 0, side6 = 0;
Random randNum = new Random();
public Dice()
{
}
public int Side1
{
get { return side1; }
set { side1 = value; }
}
public int Side2
{
get { return side2; }
set { side2 = value; }
}
public int Side3
{
get { return side3; }
set { side3 = value; }
}
public int Side4
{
get { return side4; }
set { side4 = value; }
}
public int Side5
{
get { return side5; }
set { side5 = value; }
}
public int Side6
{
get { return side6; }
set { side6 = value; }
}
public void RollDice()
//RollDice = randNum.Next(1, 7)
{
switch (randNum.Next(1, 7))
{
case 1: side1++;
break;
case 2: side2++;
break;
case 3: side3++;
break;
case 4: side4++;
break;
case 5: side5++;
break;
case 6: side6++;
break;
}
}
public override string ToString()
{
return " Freq. Rolls " +
"________________________________________" +
"\nSide 1 of Die rolled :" + side1 +
"\nSide 2 of Die rolled :" + side2 +
"\nSide 3 of Die rolled :" + side3 +
"\nSide 4 of Die rolled :" + side4 +
"\nSide 5 of Die rolled :" + side5 +
"\nSide 6 of Die rolled :" + side6 +
"\n";
}
}
}
I had an idea on how to do loop it but Im still unsure. I thought of something like this but it doesnt work and I was hoping you guys could help me!!
int howMany = int.Parse(getInfo("How many times would you like to roll the die?"));
do
{
Dice aDice = new Dice();
for (int counter = howMany; counter > 0; counter--)
{
aDice.RollDice();
}
while (howMany < 0)
{
Console.WriteLine(aDice);
}
Console.Clear();
Console.WriteLine("Session Number: {0}", sessionNumber);
Console.WriteLine(aDice);
playAgain = getYorN("Would you like to play again?");
sessionNumber++;
}
All you need to do is call aDice.RollDice method howMany times:
while (continueRunning)
{
int howMany = int.Parse(getInfo("How many times do you want to roll the die?"));
Dice aDice = new Dice();
for(int i = 0; i < howMany; i++)
{
aDice.RollDice();
}
Console.Clear();
Console.WriteLine("Session Number: {0}", sessionNumber);
Console.WriteLine(aDice);
continueRunning = getYorN("Would you like to run again?");
sessionNumber++;
Console.Clear();
}

Categories