Sunday, August 4, 2013

While/Do Loops

This is very very very important to understand if you want to make games.


int[] intArray = {3, 5, 2, 6};

int count = 0;

while (count < intArray.Length) 
       { 
       Console.WriteLine("index " + count + " = " + intArray[count]);//See how I passed that into the parameter?
       count++;
       }
            Console.WriteLine("End Program");

            Console.Read();

NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
            string action = "Quit";
            
            //Note that these are just options display or instructions and the actions are called in the switch statements
     Console.WriteLine("What would you like to do?");
     Console.WriteLine("1 - Attack?");
     Console.WriteLine("2 - Run?");
     Console.WriteLine("3 - Defend?");
     Console.WriteLine("4 - Quit?");


     do     
     {
         action = (Console.ReadLine());
         switch (action)
            { 
                case "attack":
                    Console.WriteLine("You Attacked!");
                    break;
                case "defend":
                    Console.WriteLine("You Defended!");
                    break;
                case "run":
                    Console.WriteLine("You Ran!");
                    break;
                case "quit":
                    Console.WriteLine("Good bye!");
                    break;
                default:
                    Console.WriteLine("Invalid input");
                    break;
            }
       } while (action != "Quit");
// do will check at this point to see if the program should continue or if it should end.

No comments:

Post a Comment