0

I was creating an averaging program in C# just to make sure I got array's.

What I'm trying to do is ask the user if they want to average whole numbers, or non whole numbers. I thought an If statement would be appropriate.

But the problem is, I dont know what operator to use for MULTIPLE conditions, I think this is best explained by seeing the code. (Even I'm getting confused now D:)

Console.WriteLine("\n Do you want to average whole numbers, or non-whole numbers? \n");

        if (Console.ReadLine() == "whole numbers" && Console.ReadLine() == "Whole numbers")
        {

            Console.WriteLine("You chose whole numbers.");

        }

My confusion occurs at the "Condition" && "Condition2" bit. I don't know if && is the correct operator, and C# certainly doesn't think so!

I've looked across the C# MSDN Operator list, but couldn't really understand which one to use...

P.S: The whole reason I'm doing the multiple conditions is because of capital letters ETC, if any of you knew how to combat that with a way to disregard caps and other error prone user ticks, then please tell me :D

1
  • && is the conditional AND operator. Result is true if both conditions are true. Based on your problem, you need to use || (conditional OR) operator. Commented Mar 27, 2015 at 17:18

2 Answers 2

2

Console.ReadLine reads input from the console. When the user presses enter, it returns a string. In your code it will ask twice from user to enter something if the first condition was true. So, the other one is redundant.Because you have used && operator, which is conditional AND operator, but you must use conditional OR statement(||). So, you have two choice:

In first case, you can check as many conditions as you can. But of course these are redundant. You can compare entered string with whole numbers case insensitively with the second approach:

 if (string.Equals(Console.ReadLine(), "whole numbers", StringComparison.CurrentCultureIgnoreCase))
 {
       Console.WriteLine("You chose whole numbers.");
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate, I was confused :P
0

Rather than testing for every variation in capitalization, maybe consider using the String.ToLower() method to convert the string for comparison to lowercase. Basically, by calling this method, the string that you use to call the method is converted to all lowercase letters. By using this, you make your program less prone to failure by means of capitalization.

Example:

string UserDecision = (Console.ReadLine()).ToLower();

if(UserDecision == "whole numbers")
{ // perform task }

In my example, I used Console.ReadLine() to get the user's input and then I immediately converted it to lowercase before storing it into the variable. This makes it much easier to compare strings.

In the case of which operator to use:

|| is the OR operator (ONLY ONE condition must be true)
&& is the AND operator (BOTH conditions must be true)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.