Public Group

# C# OR

This topic is 4049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

is there an or statement in C#? because I tried all of the following and they didn't work:
if(a=1 || a=2)
if(a=1) || (a=2)
if(a=1 or a=2)
if(a=10 or (a=2)
if(a=1||a=2)
if(a=1)||(a=2)
if(a=1)or(a=2)


So, is there an or statement in C#?

##### Share on other sites
Your first example works, however, you're not using a double equals, and are thus assigning the value of 1 and/or 2 to the variable 'a', which is likely screwing up your testing. Gotta be careful with that, as technically the syntax is correct (even if it doesn't do what you intend it to do). Should look like this:

if(a == 1 || a == 2) { ... }

##### Share on other sites
hahaha, wow... I am such a noob. :)

##### Share on other sites
Quote:
 Original post by Omega147Your first example works, however, you're not using a double equals, and are thus assigning the value of 1 and/or 2 to the variable 'a', which is likely screwing up your testing. Gotta be careful with that, as technically the syntax is correct (even if it doesn't do what you intend it to do). Should look like this:if(a == 1 || a == 2) { ... }

Not to mention C# doesn't allow statements like a=1 to be concluded as a true or false like C and C++ so the compiler will complain if you have

if (a=1) ...

##### Share on other sites
>:-|
OK guys, I got that problem solved... I just wasn't thinking. but here is the deal. There are 2 problems with the following code:
        static void NewGame()        {            CharacterSetup();        }        static void CharacterSetup()        {            Console.Clear();            Console.WriteLine("What will your Characters name be?");            string name = Console.ReadLine();            Console.Write("Are you sure you want to name your character '");            Console.Write(name);            Console.Write("'? Press Backspace to rename your character or enter to continue");            ConsoleKeyInfo y = Console.ReadKey();            string ys = y.ToString();            if (ys == "Backspace")            {                NewGame();            }            Character Hero = new Character();            Hero.name = name;            Hero.exp = 0;            Hero.armor = "rags coat";            Hero.health = 20;            Hero.level = 1;            Hero.strength = 5;            Console.Clear();            Console.WriteLine("What proffesion will your character have?");            Console.WriteLine("-------------------------------------------");            Console.WriteLine("1.) Warrior");            Console.WriteLine("2.) Mage");            Console.WriteLine("3.) Archer");            Console.WriteLine();            Console.WriteLine("You must press either 1, 2, or 3. Otherwise your character will be automatically assigned to 'Warrior' type.");            y = Console.ReadKey();            ys = y.ToString();            string type="";            switch (ys)            {                case "D1":                case "NumPad1":                    type = "Warrior";                    break;                case "D2":                case "NumPad2":                    type = "Mage";                    break;                case "D3":                case "NumPad3":                    type = "Archer";                    break;            }            if (type == "")            {                type = "Warrior";            }            Hero.type = type;                Hero.weapon_lvl = 1;                switch (type)                {                    case "Archer":                        Hero.weapon = "weak bow";                        break;                    case "Mage":                        Hero.weapon = "Slightly magical stick";                        break;                    case "Warrior":                        Hero.weapon = "Rusty spoon";                        break;                }            }
This code sets up all the stats for a new character in my RPG.
(text based in the console.) all of the class stuff works like a charm, my onlye problems are this:

1.) Whenever I hit backspace on the "are yyou sure" prompt, it goes on to the what is your proffesion prompt anyway. What am I doing wrong? how do I get it to restart the current function?

2.) This is more of a nitpicky thing, but I would still like to know how to fix it: on the string name=ReadLine(); It requires me to press enter twice before it goes to the next command (IE: Console.Write("Are you..."); ). Why is this?

##### Share on other sites
Quote:
 Original post by mohaakilla511.) Whenever I hit backspace on the "are yyou sure" prompt, it goes on to the what is your proffesion prompt anyway. What am I doing wrong? how do I get it to restart the current function?

When you're checking if the input equals "Backspace," your if statement will execute the new game command if true, but will then continue to the code beyond that if statement since there is no break point. You should add an else statement that processes whenever backspace wasn't hit (meaning, when backspace is it, it does not process the code contained within the "else" block)--I prefer to check for the negative case first, but, that's by preference. Your comparison for the backspace key though should use the Equals() string function for comparisons. I would also return a boolean value from the character setup method, which could then be checked by the new game function. If true, the process completed, if false, it needs to repeat. I'll let you ponder how to work all that out.
Quote:
 Original post by mohaakilla512.) This is more of a nitpicky thing, but I would still like to know how to fix it: on the string name=ReadLine(); It requires me to press enter twice before it goes to the next command (IE: Console.Write("Are you..."); ). Why is this?

Not sure. Your code is a bit tangled, but I'm sure if you work with it a little you'll eventually get this small kink out of the way.

##### Share on other sites
It has to have something to do with the readLine comand itself, becuase if I replace:
with:

then everything works just fine, except for the fact that the name that is returned is always a number.. Like 103..

##### Share on other sites
Console.Read() returns the ASCII code of the first character, hence why you're getting a number as opposed to a string. Even if you were to cast it, it would still be the first letter - not really useful in this situation, eh?

Anyway, I copied some of your code into a compiler, and I don't have to do the whole 'double enter' thing, so I'm not sure on how to solve it. Is the code the same as you posted?

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 14
• ### Forum Statistics

• Total Topics
632155
• Total Posts
3004477

×