Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The below code is giving me error "Cannot assign to 'writeline' because it is a 'method group;" at every Console.Writeline statement. I am trying to find out why but I could not find anything to point me in right direction. Any help appreciated.
{
class FizzBuzz
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
if ((i % 3 == 0) && (i % 5 == 0))
{
Console.WriteLine = "Fizzbuzz" ;
}
else if (i % 3 == 0)
{
Console.WriteLine = "Fizz";
}
else
(i % 5 == 0)
{
Console.WriteLine = "Buzz";
}
System.Console.ReadLine();
}
}
}
You need to set the string in parenthesis:
Console.WriteLine("This string goes to console.");
You tried to assign the method with a value, that is not possible. That works only with properties and fields.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am writing this function, however it gives me: The name 'binarySearchRecursive' does not exist in the current context. I dont understand why i am getting this message, the call is done in the scope of this method itself? Or do i misunderstand a fundamental part of c#?
public static int binarySearchRecusive<T>(T[] a, int low, int high, T v) where T : IComparable
{
if (low < high)
{
var middle = (low + high) / 2;
if (a[middle].CompareTo(v) == 0)
return middle;
if (a[middle].CompareTo(v) < 0)
return binarySearchRecursive(a, low, middle - 1, v);
else
return binarySearchRecursive(a, middle+1, high - 1, v);
}
return -1;
}
Your function goes by this name:
binarySearchRecusive
If you change it to:
binarySearchRecursive
it will work.
Typpo in binarySearchRecusive, instead, try binarySearchRecursive.
If it still don't work then try Program.binarySearchRecursive()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
There is some problem in my while loop, and I don't know what the problem is:
double d = 0;
while (!Double.TryParse(Console.ReadLine(), d <= 20 && d >= 1, out d))
{
Console.WriteLine("The number is incorrect, please write in again");
}
double d = 0;
while (!Double.TryParse(Console.ReadLine(), out d) && d <= 20 && d >= 1)
{
Console.WriteLine("The number is incorrect, please write in again");
}
Like this. You accidentally passed the d <= 20 && d >= 1 as an argument for the TryParse, instead of using it as an additional condition for the while loop.
Because you're misunderstanding tryparse, d is set after try parse, then you can compare it, you have put the boundaries check in the arguments of try parse:
while (!Double.TryParse(Console.ReadLine(), out var d) || d <= 20 && d>=1)
{ //you first check if you can parse it, and after parse is true, check if value is in your boundaries
Console.WriteLine("The number is incorrect, please write in again");
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Sorry if this is a dumb beginner question but I'm completely stumped.
int i = 0;
if (i == 0)
i++;
i++;
if (i == 3)
i += 2;
i += 2;
Console.WriteLine(i);
Ok, my logic, so if i = 0, add 1, then add 1 to that. So in the end i = 2.
Except it's not, it prints out 4.
The only way that could happen is if it went trough the second "if statement". Right?
What am I missing?
Yes, it's 4, let's format the code (put right indents) and see:
int i = 0; // i == 0
if (i == 0) // i == 0
i++; // i == 1
i++; // i == 2
if (i == 3) // i == 2
i += 2; // doesn't enter (since i != 3)
i += 2; // i == 4
You need to use curly brackets { } for anything more than a one line conditional
or only the first line of code after it will get executed when the conditional is true.
/*
for example would be
if (i == 0)
{
i++;
i++;
}
*/
int i = 0;
//this is true
if (i == 0)
i++; // so only this line gets executed i = 1
i++; // this will get executed no matter what. i = 2
//at this point i = 2 so the conditional is false
if (i == 3)
i += 2; // this line doesn't get executed
i += 2; /* this is not in curly brackets { } so it will get executed no matter what the conditional returns as .. so i = 4*/
//i = 4
Console.WriteLine(i);
//and that's what prints
check this out
https://www.learncs.org/en/Conditionals
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have problem with for loop in C#.
I have the following code, in main method:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for(int i=0;i>20;i++)
{
if(i%2==0)
{
Console.WriteLine("{0},", i);
}
}
Console.ReadLine();
}
}
}
The for loop does not execute. Why?
Change condition, > to <:
...
for (int i = 0; i < 20; i++) // i < 20, not i > 20
...
In your original code you assign int i = 0 then check i > 20 get false and do not enter the loop at all
The following statement in your code:
for (int i = 0; i > 20; i++)
First assigns 0 to i then evaluates the condition i > 20 which is wrong, so the for block can not be executed.
Other people have pointed out the fact that the test should actually be "i < 20" rather than "i > 20) but just to make one other point: if all you're trying to do is write out all the even numbers, you can actually increment "i" by 2 every time - that way you never have to test to see if it's even (just write "i += 2" instead of "i++").
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I am new to C#, and need a little help, i know in PHP that i can to something like this
$SelectedEvent="event selected";
if (i > 0)
{
$SelectedEvent = "event";
}
It replace a string easy, but in c# that is not working ok, here is example code i have that is not working
string SelectedEvent = "event selected";
if (i > 0)
{
SelectedEvent = "event";
}
This is not working like in PHP, i can not override the varible?
Here is my example code
EDITED
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
string SelectedEvent = "event";
}
}
You should not redeclare the variable twice but only modify its value:
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
SelectedEvent = "event";
}
// here you can use the SelectedEventVariable
// its value will be "event selected" on the first and second
// iterations and "event" on subsequent
}