why the output starts from 402 althoug the initial is zero? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
if (i%2==0)
{
Console.WriteLine(i);
}
}
}
}

It's start from 0 but you can't see.Try this:
for (int i = 0; i < 1000; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
if (i % 100 == 0) Console.ReadKey();
}
Press enter to see next numbers.That will show 100 numbers at once,you can change it if you want to display less number..

Related

No output for loop program [closed]

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 4 months ago.
Improve this question
The scope is to output the numbers from 1 to (input number) with the multiples of 3 being a * each. E.g. for 8 as input: 1,2,*,4,5,*,7,8. I tried to do this with a for loop but it has no output for some reason.
using System;
using System.Collections.Generic;
namespace HelpPleaseStackOverFlow
{
class Program
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
int x;
for (x = 1; x <= number; )
if (x % 3 == 0)
{
Console.WriteLine("*");
}
else;
{
Console.WriteLine(x);
x = x++;
}
}
}
}
I tried converting the x variable (integer) to a string and then making it the * that should be printed out, in the first half of the IF statement and the last part with the ELSE basically the same.
I also tried to make the for statement like this:
for (x = 1; x <= number; x++)
with the x++ at the end but this just game me the result of ,,8. With an input of 7; 8 should not be part of this and I have no idea why it put 8 as an output.
most of the erroros are that you are lacking some {} and ()
String input = Console.ReadLine();
int number;
int.TryParse(input, out number);
for (int x = 1; x <= number; ++x)
{
if ((x % 3) == 0)
{
Console.WriteLine("*");
}
else
{
Console.WriteLine(x);
}
}
Console.ReadLine();
Hope it helps.

Unreachable code detected in simple method with only one return [closed]

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 months ago.
Improve this question
I know there's a bunch of related questions but I only have one line returning from this method and can't see why the return line would be unreachable:
public List<int> GetMiddleLaneForAI(Room r)
{
int laneCount = GetLaneCount(true);
List<int> matches = new List<int>();
for(int x = 0; 0 < 5; x++)
{
if(aiCardFrames[x].deployedCard == null)
{
if (laneCount < 3)
{
matches.Add(x);
} else
{
if (x > 0 && x < 4)
{
if (aiCardFrames[x - 1] != null && aiCardFrames[x - 1] != null) matches.Add(x);
}
}
}
}
return matches.Count > 0 ? matches : null; // unreachable code detected
}
Look closely at this line:
for(int x = 0; 0 < 5; x++)
When does this for loop exit? ;-)

Having trouble counting in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having trouble with this. How can I count a length I want input to.
Lets say I ask a user an input. He enters 10. It counts 1,2,3,4,etc... 10.
Or if users enter 5, 1,2,3,4,5 is output
Thanks.
EDIT:
I am sorry. This isn't homework. School doesn't start till next week and I am practicing.
Sorry, I should have given code.
This is what I had that does work
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i < length; i++)
{
Console.WriteLine(i);
}
I am just assuming since I am new that I did some sloppy code and am looking for maybe something cleaner. Or another point of view for it.
update your code with <=
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
You just need to change the '<' operator to '<=':
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
string length;
Console.Write("Enter Length: ");
length= Console.ReadLine();
for (int i = 1; i <= Int32.Parse(length); i++)
{
Console.WriteLine(i);
}
Console.ReadKey();

linq and 2d integer arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How make this with LINQ?
public uint[,] pole;
public uint selColor;
for (int y = 0; y < cellsamount; y++)
{
for (int x = 0; x < cellsamount; x++)
{
if (pole[x, y] == selColor)
pole[x, y] = 0;
}
}
Is this possible or better to leave it as is?
Here's how you'd do it (but I don't recommend it, it's not very sexy). Better keep your current solution.
pole = pole.Select(i =>
i.Select(j => j == selectColor ? 0 : j).ToArray()
).ToArray();
You could do something like this:
var selectedIndices =
from x in Enumerable.Range(0, cellsamount)
from y in Enumerable.Range(0, cellsamount)
where pole[x, y] == selColor
select new { x, y };
foreach (var index in selectedIndices.ToArray())
pole[index.x, index.y] = 0;

Taking average value every 2 seconds [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have a problem with arrays or something missed in these text..
my program works every 500ms and i want to read first 4 double values and take average of these values and then get next 4 double values and so on... i write something about this and can you pls look on this??
if (u_dcbus_pv_act[i] > 0 && i != 0)
{
u_dcbus_pv = u_dcbus_pv_act[i];
p_dcbus_pv = p_dcbus_pv_act[i];
}
if (i >= 3)
{
for (int j = 0; j < 4; j++)
{
total_u += u_dcbus_pv;
total_p += p_dcbus_pv;
}
average_u = total_u / 4;
average_p = total_p / 4;
u_dcbus_target = average_u;
p_dcbus_pv_avg = average_p;
}
from what I understand of your description, I would do it something like this:
/* add current samples to totals */
total_u += u_dcbus_pv_act[i];
total_p += p_dcbus_pv_act[i];
/* every fourth tick, calc average and reset totals */
if (i % 4 == 0)
{
average_u = total_u / 4;
average_p = total_p / 4;
total_u = 0;
total_p = 0;
}
u_dcbus_target = average_u;
p_dcbus_pv_avg = average_p;
i++;

Categories