int i;
int[] mArray = new int[5];
for (i = 0; i < mArray.Length; i++)
{
Console.WriteLine("Please enter a number:");
mArray[i] = Convert.ToInt32(Console.ReadLine());
if (mArray[i] >= 50 && mArray[i] <= 10)
{
Console.WriteLine("Please enter numbers only between 10 and 50.");
mArray[i] = Convert.ToInt32(Console.ReadLine());
}
}
Can't seem to get if statement to work when it has two rules in it if (mArray[i] >= 50 && mArray[i] <= 10)
But it works fine with 1 rule if (mArray[i] >= 50)
You should use || instead of &&
mArray[i] >= 50 || mArray[i] <= 10
Look at your condition:
if (mArray[i] >= 50 && mArray[i] <= 10)
You're trying to find a number which is simultaneously at least 50 and at most 10.
What number did you have in mind that would be considered invalid?
I suspect you meant:
if (mArray[i] > 50 || mArray[i] < 10)
Note that I've changed >= and <= to > and < respectively to match your message - I assume you want 10 or 50 to be valid.
Also note that you're only testing for validity once - if the user persists in entering a second bad number, you'll accept that. I suspect you want something like:
Console.WriteLine("Please enter a number:");
int value = Convert.ToInt32(ConsoleReadLine());
while (value > 50 || value < 10)
{
Console.WriteLine("Please enter numbers only between 10 and 50.");
value = Convert.ToInt32(Console.ReadLine());
}
// Now we know it's valid, so it's reasonably to put it in the array
mArray[i] = value;
Which number is bigger than 50 AND in the same moment smaller then 10? I think you should replace your && (AND) with || (OR)
No number can be larger than 50 AND smaller than 10, your condition is wrong:
if (mArray[i] >= 50 && mArray[i] <= 10)
it should be
if (mArray[i] >= 50 || mArray[i] <= 10)
It depends on how you intend to make it work
/*Pass if the value is more or equal 50 OR lesst or equal 10*/
mArray[i] >= 50 || mArray[i] <= 10
or
/*Passs if the value is between 10 and 50, including edges*/
mArray[i] >= 10 && mArray[i] <= 50
Your statement is impossible. myArray[i] has to be both larger than 50 AND smaller than 10. Did you mean:
if (mArray[i] >= 50 || mArray[i] <= 10)
?
Try changing your logic to:
if (mArray[i] >= 50 || mArray[i] <= 10)
Related
I'm looking to make a program that calculates the sum of 7 numbers. But with a twist. IF one of these variables is smaller than 84 OR greater than 184, then this one variable is counted out of the sum of the other 6 numbers.
So lets say for example:
a=50
b=51
c=90
d=91
e=92
f=93
g=94
So now it would be; c+d+e+f+g. Do I need to do this with 1000 "If" sentences?
If you are not allowed to use loops then perhaps the next easiest way it to create a function and use that instead of +. I am going to assume then that you are also not allowed to use other slightly more complex concepts like classes ect.
public int ValueTest(int iValue)
{
if ((iValueTwo > 84) && (iValueTwo < 184) {
return iValue;
}
return 0;
}
static int Main(string[] args)
{
int a=50;
int b=51;
int iResult = 0;
iResult = ValueTest(a) + ValueTest(b);
Console.WriteLine("Result is: ", iResult);
return 0;
}
I have only done two values but this should give you the idea. Also I haven't tested this code so I may have missed something small but the general idea should still be sound.
Cheers
Do I need to do this with 1000 "If" sentences?
No, just 7
int total = 0;
if(a >= 84 && a <= 184)
total += a;
.. etc
One-liner (or rather a single statement formatted to fit here):
int total = ((a >= 84 && a <= 184) ? a : 0) +
((b >= 84 && b <= 184) ? b : 0) +
((c >= 84 && c <= 184) ? c : 0) +
((d >= 84 && d <= 184) ? d : 0) +
((e >= 84 && e <= 184) ? e : 0) +
((f >= 84 && f <= 184) ? f : 0) +
((g >= 84 && g <= 184) ? g : 0);
Or you could do it with LINQ (if that's allowed):
int total = new {a, b, c, d, e, f, g}.Where(x => x >= 84 && x <= 184).Sum();
I'm attempting to make a random number generator that then selects one of three options based on the random number. I would like to use > x < as the second choice of three but it gives me an error:
Operator '>' cannot be applied to operands of type 'bool' and 'int'
Here's the code:
int rand;
Random myRandom = new Random();
rand = myRandom.Next(0, 90);
if (rand < 33)
{
}
if (33 > rand < 66)
{
}
if (rand > 66)
{
}
Well the simplest option is to use else if so you only need to check one condition anyway - which means it would handle 33 as well (currently not handled):
if (rand < 33)
{
Console.WriteLine("rand was in the range [0, 32]");
}
else if (rand < 66)
{
Console.WriteLine("rand was in the range [33, 65]");
}
else
{
Console.WriteLine("rand was in the range [66, 89]");
}
If you do need to test two conditions, however, you just want && to check that they're both true:
// I've deliberately made this >= rather than 33. If you really don't want to do
// anything for 33, it would be worth making that clear.
if (rand >= 33 && rand < 66)
If you find yourself doing this a lot, you might want to have an extension method so you can say:
if (rand.IsInRange(33, 66))
where IsInRange would just be:
public static bool IsInRange(this int value, int minInclusive, int maxExclusive)
{
return value >= minInclusive && value < maxExclusive;
}
You should use the and && operator as in
if (rand > 33 && rand < 66)
{
}
This makes sure the rand is less than AND greater then the values specified
To check the both bounds of the value you need two comparisons.
You probably want to use the <= operator instead of < in that comparison, otherwise the code will do nothing for the values 33 and 66:
if (rand < 33)
{
}
if (33 <= rand && rand < 66)
{
}
if (rand >= 66)
{
}
You can also use else to get rid of some comparisons:
if (rand < 33)
{
}
else if (rand < 66)
{
}
else
{
}
Note: You have a random value between 0 and 89, so if you want each if statement to be used in one third of the cases, you would use values 30 and 60 instead of 33 and 66.
At the moment in one of my projects that I am working on, I need to check if a value falls between a number divisible by 12 and a number divisible by 12 + 5.
if (Number >= 0 && Number <= 5) {
value = 0;
} else if (Number >= 12 && Number <= 17) {
value = 12;
} else if (Number >= 24 && Number <= 29) {
value = 24;
}
// etc...
The code above works perfectly but I feel that it could be cut down. Does anyone have an alternative way of how to achieve what I am going for but more elegantly?
Use the % operator. (See http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx)
This should work as long as Number is positive; if it's not you'll have to look into how % works for negative numbers:
if (Number % 12 <= 5) {
value = 12*(int)(Number/12);
}
I am trying to improve a list collection that i have to replace values that are divisible by two and 10 and replace everything that is divisible by two with dTwo and ten with dTen?
My code works with one divisible statment but not two.
var num = new List<string>();
for (int n = 0; n < 101; n++)
{
num.Add(n % 2 == 0 ? "dTwo" : n.ToString());
num.Add(n % 10 == 0 ? "dTen" : n.ToString());
}
Since any number that is divisible by 10 is also divisible by 2 you have to switch your addition statements, and continue with the next number if you have a number divisible by 10:
var num = new List<string>();
for (int n = 0; n < 101; n++)
{
if( n % 10 == 0)
{
num.Add("dTen");
}
else num.Add(n % 2 == 0 ? "dTwo" : n.ToString());
}
If I can I try avoid using loop controls out side of the defined construct of the actual loop, ie. I prefer to avoid using continue if I can, it sort of feels like using goto statements. For this case, I would go for the plain and simple approach which I believe is readable, maintainable and simple albeit a little more verbose.
You can switch the order of the if/else if statements to change the priority if required, in this case the n % 10 has priority
var num = new List<string>();
for (int n = 0; n < 101; ++n)
{
if (n % 10 == 0)
{
num.Add("dTen");
}
else if (n % 2 == 0)
{
num.Add("dTwo");
}
else
{
num.Add(n.ToString());
}
}
There are two approaches I would take here, the first is verbose, but conveys what you're trying to do in a very readable manner:
var num = new List<string>(101);
for (int i = 0; i < 101 ; i++)
{
if (i == 0)
{
num.Add(i.ToString());
}
else if (i % 10 == 0)
{
num.Add("dTen");
}
else if (i % 2 == 0)
{
num.Add("dTwo");
}
else
{
num.Add(i.ToString());
}
}
The second uses a more concise LINQ-y type approach, like this.
var num = Enumerable.Range(0, 101)
.Select(
n => n == 0 ? n.ToString() :
n % 10 == 0 ? "dTen" :
n % 2 == 0 ? "dTwo" :
n.ToString())
.ToList();
Note that I've also taken into account the 0 edge case, where 0 would otherwise get reported as being divisible by 10.
Which one you go for is largely up to your taste. Personally I'd go for the latter implementation, as it's concise but still conveys the intent of the code. Some very rudimentary tests I've just done shows that it'll execute faster as well.
In my program, I think my count variable is not holding the value. What do I do so that it can hold? here`s my code.
static void Main(string[] args)
{
double a;
double count = 0;
Console.WriteLine("Enter the Numbers : ");
for (double i = 1; i <= 10; i++)
{
a = Convert.ToDouble(Console.ReadLine());
if (a % 2 != 0 || a % 3 != 0 || a % 5 != 0)
{
count = count++;
}
//else
//{
// }
Console.ReadLine();
}
Console.WriteLine("The Numbers That Are divisible by 2,3,5 are : " + count);
Console.ReadLine();
}
Your mistake is the line count = count++;. This does not increase count by one. You need to use just count++;.
The expression count++ will increment the value of count by one and then return as the expression's value the original value of count. In this case the increment of count++ happens before the assignment, so count is first incremented by one, but then you assign the value of count++, that is, the original value, to count again, so it does not increase at all.
Your program lists numbers that are not divisible by any of those numbers. If you want to count numbers which aren't divisible by all of them then you need to use if (a % 2 != 0 && a % 3 != 0 && a % 5 != 0) instead. I would also suggest using integers instead of doubles if possible.
Finally, your print statement says numbers that are divisible by 2,3,5, but count is the number of numbers which are not divisible by those numbers.
Edit: Are you entering 10 numbers each time you test? I'm not sure what kind of result you will get if you give a blank input.
Not to add to what jk and David Kanarek said in their answers or what the others the comments on those answers, As pointed out by jk use count+1 instead of count ++ , also a few notes:
1) Your using console.Readline() twice in the loop, so the user will enter 20 inputs but only 10 will be read..
2) Just alittle extra thought on Anton's comment, in your if clause , if you use || your trying to catch any of the conditions being true, in other words:
// a=2
if (a % 2 != 0 || a % 3 != 0 || a % 5 != 0) // False || True || True = True
{
count = count + 1 ;// count will increase
}
on the other hand, using && :
// a=2
if (a % 2 != 0 && a % 3 != 0 && a % 5 != 0) // False && True && True = false
{
count = count + 1 ; //count will not increase
}
A useful Link explaining operators