C# While Statement

Started by
4 comments, last by cavendert 9 years, 8 months ago

This should be simple, and I'm really hung up on this here......

As you will see in the code below, I'm trying to make a simple Pokemon battle. I'm trying to create a While loop, to constantly subract Health and Attack, until somebody has 0 health.

Though the code will be written incorrectly, he is what I'm trying to do:


while (myPokemon[int parse(myChoice)-1].Health >0 && chadPokemon[randomChoice].Health>0)

This is giving me errors. I will post the rest of my current, working code below. Please look it over, and let me know what I am missing!!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Practice_Battle
{
    class Pokemon
    {
        public string Name;
        public int Health;
        public int Attack;
    }


    class Program
    {
        static List<Pokemon> chadPokemon = new List<Pokemon>() {
            new Pokemon {
                Name = "Charizard",
                Health = 200,
                Attack = 50
            },
            new Pokemon {
                Name = "Garchomp",
                Health = 180,
                Attack = 50
            },
            new Pokemon {
                Name= "Metagross", 
                Health = 200,
                Attack = 50
            },
            new Pokemon {
                Name = "Aegislash",
                Health = 200,
                Attack = 50
            },
            new Pokemon {
                Name = "Quagsire",
                Health = 200, 
                Attack = 50
            },
            new Pokemon {
                Name = "Gardevoir",
                Health = 200,
                Attack = 50
            }
        };


        static List<Pokemon> myPokemon = new List<Pokemon>() {
            new Pokemon {
                Name = "Blastoise",
                Health = 200,
                Attack = 50
            },
            new Pokemon {
                Name = "Blazekin",
                Health = 200, 
                Attack = 50
            },
            new Pokemon {
                Name = "Meganium", 
                Health= 200, 
                Attack = 50
            }
        };
        
        static void Main(string[] args)
        {
                       
            //Chad's List of Pokemon
            //string[] chadPokemon = new string[6] { "Charizard", "Garchomp", "Metagross", "Aegislash", "Quagsire", "Gardevoir" };


            //Random Pokemon Generator
            var rnd = new Random();
            var randomChoice = chadPokemon[rnd.Next(chadPokemon.Count)];
           




            Console.WriteLine("Gamer Chad wants to battle!");
            Console.WriteLine("Do you accept? (yes or no)");
            string acceptInput = Console.ReadLine();


            string message = "";


            if (acceptInput == "yes")
                message = "It's time to put your game face on!";
            else
                message = "Too bad. It's time to battle!";


            //Random Pokemon is Chosen, I choose a Pokemon
            Console.WriteLine(message);
            Console.WriteLine();              
            Console.WriteLine("I choose you, " + randomChoice.Name);
            Console.WriteLine();
            Console.WriteLine("Please choose your Pokemon:");
            Console.WriteLine("Press 1 for Blastoise.");
            Console.WriteLine("Press 2 for Blazekin.");
            Console.WriteLine("Press 3 for Meganium.");
            var myChoice = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine(myPokemon[int.Parse(myChoice)-1].Name + ", I choose you!");
           
            Console.WriteLine("Type yes to Attack.");
            acceptInput = Console.ReadLine();


            while(myPokemon.Health >0 && chadPokemon.Health>0)
            {


            }
                       
        }
    }
}
Advertisement

What exactly are the errors?

The first line of code won't compile because "myPokemon[int parse(myChoice) - 1]" isn't a valid expression. You probably mean "int.Parse(myChoice)" not "int parse(myChoice)."

"randomChoice" is also a Pokemon and not an integer. "randomChoice.Health > 0" instead of "chadPokemon[randomChoice].Health>0)".

Oh.........duh. Lol. I was using an example that I had from C++.

"randomChoice" is also a Pokemon and not an integer. "randomChoice.Health > 0" instead of "chadPokemon[randomChoice].Health>0)".

This makes much more sense. My only question to that is, will randomChoice.Health remember what it chose earlier in the code, or will it run the random selection again?


or will it run the random selection again?

Why would it? randomChoice won't change unless you have code inside the while loop that changes it.

But, the best way to determine that for yourself would be for you to actually try the code and see.

Right, and that makes perfect sense. I just wanted to make sure that I fully understood it. I have a really bad habit of just doing things, and knowing that they work, and not why they work. Thanks for the information! I seem to be on the right path now. I just need to find a more efficient way to write it.

Thanks for the help, and I'll be back in a new forum if I'm stuck again.

This topic is closed to new replies.

Advertisement