Community discussion forum

Loop help needed

  • 1 year ago
    Hello i am a 1st year web tech at uni and I really am new to c#, Can anyone tell me how I add a loop onto a switch statement? Thanks
    Post was edited on 18/11/2008 19:52:21 Report abuse
  • 1 year ago
    You mean something like this? for (int i = 0; i < 10; i++) { switch (i) { case 0: // do something break; case 1: // do something else break; } }
  • 1 year ago
    I have no idea to be honest, this is my code: using System; using System.Collections.Generic; using System.Text; namespace MandysGame { class Program { static void Main(string[] args) { ////// ///This is a simple game, player enters there/// ///details when prompted to do so by the questions,/// ///which results in the story being told using there answers/// ////// String Name; String Gender; String Age; String Occupation; String Friend; String Animal; String Colour; String Flower; Console.WriteLine("Simple Game"); Console.Write("Please enter your Name? "); Name = Console.ReadLine(); Console.Write("Please enter your Gender? "); Gender = Console.ReadLine(); Console.Write("Please enter your Age? "); Age = Console.ReadLine(); Console.Write("Please enter your Occupation? "); Occupation = Console.ReadLine(); Console.Write("Please enter the name of a Friend? "); Friend = Console.ReadLine(); Console.Write("Please enter your favourite Animal? "); Animal = Console.ReadLine(); Console.Write("Please enter your favourite Colour? "); Colour = Console.ReadLine(); Console.Write("Please enter your favourite Flower? "); Flower = Console.ReadLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Secret Agent: 008"); Console.WriteLine("Name: {0}", Name); Console.WriteLine("Gender: {0}", Gender); Console.WriteLine("Age: {0}", Age); Console.WriteLine("Assumed Occupation: {0}", Occupation); Console.WriteLine("Partner Agent: {0}", Friend); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Your mission should you choose to accept will be to "); Console.WriteLine("deliver a top secret message to the prime minister "); Console.WriteLine("Do you except your mission? (yes/no)"); string input = Console.ReadLine(); switch (input.ToLower()) { case "yes": Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Great! Take this letter to the prime minister,"); Console.WriteLine("I want you to both wear {0} {1}s.", Colour, Flower); Console.WriteLine("Please be sure to take a {0} with you to disguise who you are.", Animal); Console.WriteLine("When you have completed your mission, you are free"); Console.WriteLine("to carry on in your assumed role as a {0}.", Occupation); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Good Luck agent 008"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Program Terminated"); break; case "no": Console.WriteLine("Too bad, you are not what we expect in an agent!!"); Console.WriteLine(); Console.WriteLine("Program terminated"); break; default: Console.WriteLine("I'm sorry, that is incorrect, goodbye!"); Console.WriteLine(); Console.WriteLine("Program terminated"); break; } } } } Thanks Mandy
  • 1 year ago
    I have never done a loop so i really dont know what to do, or which loop I should use at all, but my tutor said it would be good to add one so that if the player selects no or enters an incorrect answer the game starts again or when the person has played the game it starts again. Thanks Mandy
  • 1 year ago
    can anyone help?
  • 11 months ago
    Hi, I have never done C# console programming, so not sure to its limitations. but I would put the 'Game' code into a seperate routine and have you main routine fire the game procedure, when it returns you can ask the user if they wish to play again. so:- While the user answers 'Yes' to 'Play game?' Game procedure loop for specifics of loops etc.. check out the MSDN (assuming you have a copy at uni)

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!