Loop help needed

loop , csharp , web tech , student , help Rotherham, United Kingdom
  • 12 years 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
  • 12 years 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; } }
  • 12 years 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
  • 12 years 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
  • 12 years ago
    can anyone help?
  • 12 years 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).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer Science is no more about computers than astronomy is about telescopes.” - E. W. Dijkstra